Hi,
I have a problem with reading a textfile.
this textfile looks like that:
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:
#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...
Convert ANSI strings to WCHAR before copying it to structure.
Thank you for your answer @timovjl
Could You please give me a little help therefore?
I never used that before... ???
Test these changes with PPC:
wchar_t *fmt = L"%d %200[^\n]\n %200[^\n]\n";
if (n != fwscanf(fp, fmt, &buttons[i].nr, buttons[i].text, buttons[i].link))
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???
OK, I found my mistake myself... ;D
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()...
2
Windows Explorer starten
\\Windows\\Explorer.exe
This link works fine, but all links like this don´t:
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"? ???
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?!?
some code for getting error string.
#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.
I tried it like that, but no MsgBox came up.
But if i debug it like that:
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).
ODS: Error 2
ODS: The system cannot find the file specified.
... 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:
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 ???
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