Problem - TCHAR as LPARAM to a window that belongs

2019-07-28 07:48发布

问题:

So i am playing/implementingtomyown with windows via c book examples and there is something about dll injection part that boggles me and i can't solve it.

I created a dialog that belongs to another thread/process and i am trying to send it TCHAR variable so it can then use that var in some function(both the function and tchar are in the same dll file)

So when the dialog is created and sitting well in another thread i send it a message.

First i declare tchar

TCHAR finalpath[MAX_PATH];

Then later i just fill it with info( i do this in the dll thread, not in the dialog's thread, let me also mention that i must do this in the dll thread because thats only way to fill the required tchar(i am required to get dll working directory and fill it in tchar))

So, when i get this info in my tchar i am trying to send a message to the dialog and use tchar as LPARAM(wparam is hwnd btw)

SendMessage(hWndDIPS, WM_APP, (WPARAM) lista, (LPARAM)finalpath); 

Afterwards i do basic schoolwork in another threads dialog procedure loop...

INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

   switch (uMsg) {
      chHANDLE_DLGMSG(hWnd, WM_CLOSE, Dlg_OnClose);

      case WM_APP:


  SaveListViewItemPositions((HWND) wParam, (TCHAR)lParam);
         break;
   }

   return(FALSE);
}

Function that is supposed to receive the parameter(this function resides in shared dll and is called by the procedure as you see above is defined as follows..

void SaveListViewItemPositions(HWND hWndLV, TCHAR sejv[]) { ...}

The compiler error i get from this is

Error 7 error C2664: 'SaveListViewItemPositions' : cannot convert parameter 2 from 'TCHAR' to 'TCHAR []'

So i have no idea why is this happening. If tchar is array then i need to use it in parameters with [] added as thats how arrays are used in parameters(not to mention that if i dont do it it gives me more errors and i cant use the parameter in function anyways)

So why is it not converting then?

If there is another solution to make this dialog to receive a tchar var then please explain.

Thanks

回答1:

I think that your problem is that you're typecasting the LPARAM to a TCHAR instead of an array of TCHARs (TCHAR*). Try changing that and see if it fixes things.



回答2:

Even after you'll fix your type declarations and properly cast the LPARAM to a TCHAR*, your code will be incorrect. The 'parameter' you pass in to that window procedure is a pointer, and as any pointer, is only valid within a process address space. The receiver window will have to use ReadProcessMemory and copy the string from your process into its own process. Of course, this implies that the receiver process knows your process id, and has proper privileges to be able to read from your memory. And you also need to pass in the length of the string, since ReadProcessMemory cannot guess where the NULL terminator is (although I reckon that with a MAX_PATH max length, this is not a serious issue).

So you are correct, this is a headache, and more so down the road. The privilege issue may be a show stopper.

There are several IPC mechanisms you could use. An easy one is an anonymous named pipe, see Anonymous Pipe Operations. Shared memory is another, see Using Shared Memory in a Dynamic-Link Library. COM would also work (have the process you 'control' create an instance of a class that is hosted in your process server, and let the COM marshaling do the rest, see Marshaling Details). Or you could hand-marshal a COM interface between the process boundary (see CoMarshalInterface).