dllimport 如何在非托管 dll 中获取哪个应用程序调用的函数

dllimport how to get which application called function in unmanaged dll

我有 1 个具有 dllexport 函数的非托管 C++ dll3 个托管 C# 应用程序将非托管 dll 函数与 dllimport 一起使用,好的,一切正常。

我需要知道在 C++ dll 中,我怎样才能得到一个值,告诉我哪个 C# 应用程序从外部调用它。

请看下图:

  • now,when client.exe calls function1 from unmanaged dll , a message shows up "client.exe called me!"
  • now,when console.exe calls function1 from unmanaged dll , a message shows up "console.exe called me!"
  • now,when pure.exe calls function1 from unmanaged dll , a message shows up "pure.exe called me!"

我需要非托管库自己找到它,我不想从 C# 应用程序发送任何额外的东西,比如获取 C# 文件路径并将其发送到函数。

这可能吗?如果是,我该怎么做?

这应该有效:

constexpr DWORD bufsize = 32768;
WCHAR *calling_application = new WCHAR [bufsize];
DWORD nSize = GetModuleFileNameW (NULL, calling_application, bufsize);

if (nSize == 0 || nSize == bufsize)
{
    // panic, should never happen
}

// ...

delete [] calling_application;

文档在这里:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197%28v=vs.85%29.aspx