• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1428
  • Last Modified:

vc++ unicode messagebox char issue

i need to pass path information from one event to another, but the char that i use for that gets 'garbled' in the process

i.e. for the code below, the first Messagebox shows me a file path as i need, whereas the second messagebox shows a lot of weird characters. ...

whats going on, is this to do with the fact that my project is compiled in unicode?
char *currentSongPath;
 
LRESULT CALLBACK WinampWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) {
	int ret = CallWindowProc(proc,hwnd,umsg,wParam,lParam);
 
	if (umsg == WM_WA_IPC && lParam == IPC_PLAYING_FILE) { 
		// File Path and Name about to be played right now
 
		currentSongPath = (char*)wParam;
		MessageBoxA(NULL,currentSongPath,"This outputs a file path as it should", MB_OK);
	}
 
	if (umsg == WM_WA_IPC && lParam == IPC_GET_NEXT_PLITEM) {
		// res will be millisecond into play when next is played or if song finishes
		
		MessageBoxA(NULL, currentSongPath,"This same line outputs a garbled thing", MB_OK);
}
 
	return ret;
}

Open in new window

0
KristjanLaane
Asked:
KristjanLaane
  • 2
  • 2
1 Solution
 
jkrCommented:
No, but in the 2nd case, you are using an uninitialized pointer variable, it is only being assigned a value in the 1st 'if' body. Try
char *currentSongPath;
 
LRESULT CALLBACK WinampWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) {
        int ret = CallWindowProc(proc,hwnd,umsg,wParam,lParam);
 
        if (umsg == WM_WA_IPC && lParam == IPC_PLAYING_FILE) { 
                // File Path and Name about to be played right now
 
                currentSongPath = (char*)wParam;
                MessageBoxA(NULL,currentSongPath,"This outputs a file path as it should", MB_OK);
        }
 
        if (umsg == WM_WA_IPC && lParam == IPC_GET_NEXT_PLITEM) {
                // res will be millisecond into play when next is played or if song finishes
                
                currentSongPath = (char*)wParam; // assign here, too
                MessageBoxA(NULL, currentSongPath,"This same line should be OK now", MB_OK);
}
 
        return ret;
}

Open in new window

0
 
KristjanLaaneAuthor Commented:
this does not work as wParam holds completely different values for the differnet events.

and the event where lParam == IPC_PLAYING_FILE always occurs before the event where lParam == IPC_GET_NEXT_PLITEM, so currentSongPath shoudl be initialised anyway when the event lParam == IPC_GET_NEXT_PLITEM is raised

and im not sure this is an initialisation problem, i think the garbage might be to do with character encoding ...
0
 
jkrCommented:
>>and im not sure this is an initialisation problem

It pretty certainly is, look at your code:
char *currentSongPath;
 
LRESULT CALLBACK WinampWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) {
        int ret = CallWindowProc(proc,hwnd,umsg,wParam,lParam);
 
        if (umsg == WM_WA_IPC && lParam == IPC_PLAYING_FILE) { 
                // File Path and Name about to be played right now
 
                currentSongPath = (char*)wParam;
                MessageBoxA(NULL,currentSongPath,"This outputs a file path as it should", MB_OK);
        }
 
        if (umsg == WM_WA_IPC && lParam == IPC_GET_NEXT_PLITEM) {
                // res will be millisecond into play when next is played or if song finishes
                
                //
                // here, 'currentSongPath' is not assigned a value
                // and the last value from a previous call is no 
                // longer valid. You need to initialize this variable
                // somehow or you'l get 'garbage'
                //
                MessageBoxA(NULL, currentSongPath,"This same line outputs a garbled thing", MB_OK);
}
 
        return ret;
}

Open in new window

0
 
xtravaganCommented:
If you look closer you will see that the variable is outside the scope of the function and will thus hold the last assigned value.

However the problem is that the wParam is lost after the message is handled. What you need to do is this


char currentSongPath[256];
LRESULT CALLBACK WinampWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) {
        int ret = CallWindowProc(proc,hwnd,umsg,wParam,lParam);
 
        if (umsg == WM_WA_IPC && lParam == IPC_PLAYING_FILE) { 
                // File Path and Name about to be played right now
 
                strncpy(currentSongPath, sizeof(currentSongPath) - 1, (char*)wParam);
                currentSongPath[sizeof(currentSongPath) - 1] = 0;
                MessageBoxA(NULL,currentSongPath,"This outputs a file path as it should", MB_OK);
        }
 
        if (umsg == WM_WA_IPC && lParam == IPC_GET_NEXT_PLITEM) {
                // res will be millisecond into play when next is played or if song finishes
                
                MessageBoxA(NULL, currentSongPath,"This same line outputs a garbled thing", MB_OK);
}
 
        return ret;
}

Open in new window

0
 
KristjanLaaneAuthor Commented:
i got this error message when i tried to compile
Error    6    error C2664: 'strncpy' : cannot convert parameter 2 from 'size_t' to 'const char *'    e:\Code\SoundCutPrototype\SoundCut4Winamp\main.cpp    86    gen_blah

then realised the 8th line had a syntax error, and when i fixed it to below, it works ! thanks !!!

strncpy(currentSongPath, (char*)wParam, sizeof(currentSongPath) - 1);

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now