NO

Author Topic: Problem in creating a individual Menu  (Read 4593 times)

pitter2206

  • Guest
Problem in creating a individual Menu
« on: December 05, 2010, 02:37:37 PM »
Hi,
I have a problem with reading a textfile.

this textfile looks like that:
Code: [Select]
1
"Test1"
"\My Flash Disk\test.exe"
.
.
.
8
"Test2"
"\My Flash Disk\test.exe"

The first entry is the button-number,
the second has to be the button-text
and the third entry has to be the command, which starts the application shown there.

I tried a lot, but neither the button-Text is shown mor the link will executed...

This is the code:
Code: [Select]
#include <windows.h>
#include <windowsx.h>
#include "main.h"
#include <string.h>
#include <stddef.h>

#define LINELEN_MAX 200 // Maximale Anzahl der Zeichen einer Zeile.
#define N 8 // Anzahl der Button.

static  INT_PTR CALLBACK MainDlgProc(HWND, UINT, WPARAM, LPARAM);
static HANDLE ghInstance;

typedef struct button Button;

struct button
 {
   int nr;
    wchar_t text[LINELEN_MAX+1];
    wchar_t link[LINELEN_MAX+1];
};

void view_button_array ( Button* b, unsigned n )
 {
    unsigned i = 0;
    while ( i < n )
       printf ( "%d\n" "%s\n" "%s\n\n", b[i].nr, b[i].text, b[i].link ), i++;
}


int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
{
    WNDCLASS wc;

    HWND hwnd;
    hwnd = FindWindow(L"Addins_Menu.class", NULL);    // Nachschauen, ob der Prozess schon läuft
    if (hwnd) // wenn der Prozess läuft
    { //
        SetForegroundWindow((HWND)((ULONG)hwnd|0x00000001)); // Fenster in den Vordergrund bringen
        return 0;
    }

    ghInstance = hInstance; //
    if (!GetClassInfo(NULL, L"Dialog", &wc))
        return 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_ICO_MAIN));
    wc.lpszClassName = L"Addins_Menu.class";
    if (!RegisterClass(&wc))
        return 0;

return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)MainDlgProc);
}

static  INT_PTR CALLBACK MainDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    int n = 3, // Anzahl der zu einem Datensatz zugehörigen Zeilen.
        i = 0; // Index und Zähler fürs Strukturarray.
    char* fname = "My Flash disk\\navigation\\skin480x272\\Addins\\Addins.txt",
           *fmt = "%d %200[^\n]\n %200[^\n]\n"; // Angepasst an LINELEN_MAX.
    FILE* fp = fopen ( fname, "r" );
Button buttons[N] = {0}; // Strukturarray für die Daten der Buttons.
   
if ( fp == NULL )
{
MessageBox(NULL, L"Addins.TXT fehlt!", L"Addins-Menü", MB_SETFOREGROUND);
return (0);
}

    while ( !feof(fp) )
{
        if ( i == N )
{
MessageBox(NULL, L"Addins.TXT beschädigt! ", L"Addins-Menü", MB_SETFOREGROUND);
return (0);
            break;
        }
        if ( n != fscanf ( fp, fmt, &buttons[i].nr, buttons[i].text, buttons[i].link ))
{
  MessageBox(NULL, L"Addins.TXT nicht vollständig! ", L"Addins-Menü", MB_SETFOREGROUND);
return (0);
            break;
        }
        i++;
    }

   printf ( "Es wurden %d Datensaetze eingelesen.\n\n", i );
   view_button_array ( buttons, i );
    fclose ( fp );

HWND button1 = CreateWindow(_T("BUTTON"),_T(" "), WS_VISIBLE|WS_TABSTOP|WS_CHILD, 0, 80,72, 40, hwndDlg, (HMENU) Button1, NULL, NULL);
PostMessage(button1,WM_SETTEXT, 0, (LPARAM)buttons[0].text);
        /* and so on until button8
.
.
. */
   switch (uMsg)
    {
        case WM_COMMAND:
            switch (GET_WM_COMMAND_ID(wParam, lParam))
            {
case Button1:
CreateProcess(buttons[1].link,NULL,0,0,FALSE,0, 0, 0, NULL, NULL);
PostQuitMessage(0);
EndDialog;
return (0);
        /* and so on until button8
.
.
.*/

If I use SendMessage, the script lags...
Please help me for fixing my problem with this script...


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Problem in creating a individual Menu
« Reply #1 on: December 05, 2010, 04:49:22 PM »
Convert ANSI strings to WCHAR before copying it to structure.
May the source be with you

pitter2206

  • Guest
Re: Problem in creating a individual Menu
« Reply #2 on: December 05, 2010, 06:07:27 PM »
Thank you for your answer @timovjl

Could You please give me a little help therefore?
I never used that before...  ???

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Problem in creating a individual Menu
« Reply #3 on: December 06, 2010, 12:17:36 PM »
Test these changes with PPC:
Code: [Select]
wchar_t *fmt = L"%d %200[^\n]\n %200[^\n]\n";
Code: [Select]
if (n != fwscanf(fp, fmt, &buttons[i].nr, buttons[i].text, buttons[i].link))
« Last Edit: December 06, 2010, 12:26:38 PM by timovjl »
May the source be with you

pitter2206

  • Guest
Re: Problem in creating a individual Menu
« Reply #4 on: December 06, 2010, 01:07:08 PM »
Thanks @timovjl,

this will work, if I change PostMessage() to SendMessage()...

But... If I only change one PostMessage() to SendMessage(), the script lags... To open the window it lasts about 15 sec....
and a click at a button lags also...
If I change all buttons, the windows doesn´t open.  ???

Do you have any idea, why that script lags???

pitter2206

  • Guest
Re: Problem in creating a individual Menu
« Reply #5 on: December 06, 2010, 09:34:51 PM »
OK, I found my mistake myself...  ;D

Code: [Select]
CreateWindow(_T("Button"), buttons[0].text, WS_VISIBLE|WS_CHILD|BS_MULTILINE, 0, 80,72, 40, hwndDlg, (HMENU) Button1, NULL, NULL);
CreateWindow(_T("Button"), buttons[1].text, WS_VISIBLE|WS_CHILD|BS_MULTILINE, 80, 80, 72, 40, hwndDlg, (HMENU) Button2, NULL, NULL);

Edit:

New probs with CreateProcess()...

Code: [Select]
2
Windows Explorer starten
\\Windows\\Explorer.exe

This link works fine, but all links like this don´t:
Code: [Select]
1
GoPal starten
\\My Flash Disk\\navigation\\Skin480x272\\Addins\\gopal_start.exe

As you can see in the attached PNG: The link to the exe is fine.
Is ther a prob with the space in "Storage Card"? ???



« Last Edit: December 06, 2010, 11:37:38 PM by pitter2206 »

pitter2206

  • Guest
Re: Problem in creating a individual Menu
« Reply #6 on: December 07, 2010, 02:50:05 PM »
Ok...  ???

I tried GetLastError() and I got a Message:

ODS: CreateProcess failed (2).

Would it be possible, that the path includes \n?
How can I fix that prob ???

..but I wonder, why the Explorer starts, but nothing else?!?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Problem in creating a individual Menu
« Reply #7 on: December 07, 2010, 05:14:22 PM »
some code  for getting error string.
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int LastError(HWND hWnd)
{
LPVOID lpMsgBuf;
 
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

// Display the string.
OutputDebugString(lpMsgBuf);
MessageBox( hWnd, lpMsgBuf, 0, MB_OK|MB_ICONERROR );

// Free the buffer.
LocalFree( lpMsgBuf );
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpszCmdLine, int nCmdShow)
{
if (!CreateProcess(L"\\Storage Card\\ARMPE.EXE",NULL,0,0,FALSE,0, 0, 0, NULL, NULL)) {
LastError(0);
}
return 0;
}
I can get this: ODS: The system cannot find the file specified.
« Last Edit: December 07, 2010, 06:33:49 PM by timovjl »
May the source be with you

pitter2206

  • Guest
Re: Problem in creating a individual Menu
« Reply #8 on: December 07, 2010, 05:46:18 PM »
I tried it like that, but no MsgBox came up.

But if i debug it like that:

Code: [Select]
case Button2:
    if( !CreateProcess(buttons[1].link, NULL,0,0,FALSE,0, 0,0,NULL,&pi  ))
{   
printf( "CreateProcess failed (%d).\n", GetLastError() );
MessageBox(NULL,L"Programm fehlt!",L"Fehler", MB_SETFOREGROUND|MB_OK);
return 1;
}

I got this Error:

ODS: CreateProcess failed (2).



Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Problem in creating a individual Menu
« Reply #9 on: December 07, 2010, 06:37:02 PM »
ODS: Error 2
ODS: The system cannot find the file specified.
May the source be with you

pitter2206

  • Guest
Re: Problem in creating a individual Menu
« Reply #10 on: December 07, 2010, 06:50:56 PM »
... and that is my problem with it...  :'(

The specified files are there...

I tried if there would be \n at the end of the path:

Code: [Select]
  while ( !feof(fp) )
{
        if ( i == N )
{
MessageBox(NULL, L"Addins.TXT beschädigt! ", L"Addins-Menu", MB_SETFOREGROUND);
return (0);
             break;
         }
if (n != fwscanf(fp, fmtw, &buttons[i].nr, buttons[i].text, buttons[i].link))
{
  MessageBox(NULL, L"Addins.TXT nicht vollständig! ", L"Addins-Menu", MB_SETFOREGROUND);
return (0);
             break;
  }
int len = wcslen(buttons[i].link);
if(len && buttons[i].link[len-1]=='\n')
   buttons[i].link[len-1] = 0;
i++;
     }
    fclose ( fp );
but the same ODS Error 2

What the hell can I do to fix it ???
« Last Edit: December 07, 2010, 06:52:30 PM by pitter2206 »

pitter2206

  • Guest
Re: Problem in creating a individual Menu
« Reply #11 on: December 08, 2010, 12:11:59 PM »
I found my mistake...  :)

In the textfile I wrote the paths like that:
\\Storage Card\\Programme\\Testordner\\Test.exe

That was wrong!!!

The wright path has to be written without preposed backslashs:
Storage Card\Programme\Testordner\Test.exe

The script now reads so correct path to the program...  ::)

Thanks a lot for helping!!! Great forum!!!

Edit: changed from 2 Backslashs to 1... will work fine
« Last Edit: December 08, 2010, 06:02:35 PM by pitter2206 »