问题 - TCHAR 作为属于另一个进程/线程的窗口的 LPARAM

发布于 2024-10-10 08:37:59 字数 1365 浏览 3 评论 0原文

所以我正在通过c书示例在windows上玩/实现tomyown,并且有一些关于dll注入部分的东西让我感到困惑,我无法解决它。

我创建了一个属于另一个线程/进程的对话框,我试图向它发送 TCHAR 变量,以便它可以在某个函数中使用该 var(函数和 tchar 都在同一个 dll 文件中)

所以当创建对话框时,我坐在另一个线程中向它发送一条消息。

首先我声明 tchar

TCHAR finalpath[MAX_PATH];

然后我只是用信息填充它(我在 dll 线程中执行此操作,而不是在对话框的线程中,让我也提到我必须在 dll 线程中执行此操作,因为这是填充所需 tchar 的唯一方法(我需要获取 dll 工作目录并将其填充到 tchar 中))

我尝试向对话框发送消息并使用 tchar 作为 LPARAM(wparam 是 hwnd btw)

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

因此,当我在 tchar 中获取此信息时, 另一个线程对话框过程循环中的作业...

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);
}

应该接收参数的函数(该函数驻留在共享 dll 中,并由上面看到的过程调用,定义如下..

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

我从中得到的编译器错误是

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

所以我不知道为什么会发生这种情况。如果 tchar 是数组,那么我需要在参数中使用它,并添加 [],因为这就是在参数中使用数组的方式(更不用说,如果我不这样做,它会给我更多错误,而且我无论如何也不能在函数中使用该参数)

所以那为什么它不转换呢?

如果有另一种解决方案使该对话框接收 tchar var 那么请解释。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南巷近海 2024-10-17 08:37:59

即使您修复了类型声明并将 LPARAM 正确转换为 TCHAR*,您的代码仍然不正确。传递给该窗口过程的“参数”是一个指针,并且与任何指针一样,仅在进程地址空间内有效。接收器窗口必须使用 ReadProcessMemory 并将字符串从您的进程复制到其自己的进程中。当然,这意味着接收进程知道您的进程 ID,并且具有能够从您的内存中读取数据的适当权限。而且您还需要传入字符串的长度,因为 ReadProcessMemory 无法猜测 NULL 终止符在哪里(尽管我认为对于 MAX_PATH 最大长度,这不是一个严重的问题)。

所以你是对的,这是一个令人头痛的问题,而且以后更是如此。特权问题可能是一个阻碍。

您可以使用多种 IPC 机制。一个简单的方法是匿名命名管道,请参阅 Anonymous管道操作。共享内存是另一个,请参阅在动态链接库。 COM 也可以工作(让您“控制”的进程创建托管在进程服务器中的类的实例,并让 COM 封送处理完成其余的工作,请参阅 编组详细信息)。或者,您可以在进程边界之间手动编组 COM 接口(请参阅 CoMarshalInterface)。

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).

蓝眼泪 2024-10-17 08:37:59

我认为您的问题在于您将 LPARAM 类型转换为 TCHAR 而不是 TCHAR 数组(TCHAR*)。尝试改变它,看看是否能解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文