1

I have a C# Project which Invoke a C++ dll And before returning the value in the C++ dll, I would like to check the name of the C# exe which invoke my method. Can you advice me please?

I Load the c++ dll like this:

[DllImport("MindSystem.dll", 
           EntryPoint = "MindSystemPlusPlus",
           CharSet = CharSet.Ansi,
           CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] 
public static extern IntPtr MindSystemPlusPlus(int value); 

And when I load it, I want that the c++ dll check the name of the exe which invoke it

Edit: I tried this code, but the output in c# is in strange characters :

char fileName[MAX_PATH + 1];
GetModuleFileNameA(NULL, fileName, MAX_PATH + 1);
return fileName;
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
MindSystem
  • 11
  • 4
  • How is this associated with c#, your dll is written on c++ and all you can do is associated with c++? – Samvel Petrosov Jul 02 '17 at 19:29
  • I Load the c++ dll like this : [DllImport("MindSystem.dll", EntryPoint = "MindSystemPlusPlus", CharSet = CharSet.Ansi, CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] public static extern IntPtr MindSystemPlusPlus(int value); and when i load it, i want that the c++ dll check the name of the exe which invoke it – MindSystem Jul 02 '17 at 19:30
  • What should i remove the c++ tag? I'm looking for a c++ code. From now, i don't have any code in c++ – MindSystem Jul 02 '17 at 19:33
  • Possible duplicate of [How to find caller assembly name in unmanaged c++ dll](https://stackoverflow.com/questions/5459526/how-to-find-caller-assembly-name-in-unmanaged-c-dll) – Samvel Petrosov Jul 02 '17 at 19:41

4 Answers4

1

You should try using GetModuleFileName() function. You can get the full path of the exe. Keep in mind if your DLL is loaded by more than one applications then returned file path will refer to only one of them.

MKR
  • 19,739
  • 4
  • 23
  • 33
  • 1
    Multiple applications loading DLL should not be a problem because GetModuleFileName() will provide path to executable of a process that calls DLL function. I believe this is something that author of the question wants. – jacekbe Jul 02 '17 at 19:56
1

You can call GetModuleFileName function. NULL as first parameter means that path to the executable of the current process is requested.

std::string expectedPath("C:\\expected.exe");

TCHAR fileName[MAX_PATH + 1];
DWORD charsWritten = GetModuleFileName(NULL, fileName, MAX_PATH + 1);
if (charsWritten != 0)
{
    if (expectedPath == fileName)
    {
        // do something
    }
}
jacekbe
  • 499
  • 1
  • 5
  • 18
  • it's the code that i used. But how to use the result to compare it with, for example a string? – MindSystem Jul 02 '17 at 20:34
  • I tried this but it's not working : char fileName[MAX_PATH + 1]; GetModuleFileNameA(NULL, fileName, MAX_PATH + 1); return fileName; – MindSystem Jul 02 '17 at 20:56
  • You cannot return fileName as it's a character array that is local to the function where it's defined. After returning from function it is not valid anymore. You can wrap it with C++ string like this: std::string(fileName) and return it instead. And please describe in what way it's not working. What are the contents of fileName just after GetModuleFileName is called. – jacekbe Jul 02 '17 at 21:07
  • I used const char* instead of std::string but it's still unworking, this ouput is this in my case : x§Âoé«_x§Âoÿ^¤Øæí2öpÿÿÿÿDçí XLqÿÿÿÿ0öp*/öp¤÷p – MindSystem Jul 02 '17 at 21:25
  • If you are trying to read this returned string on C# side then it will fail. Please look up how to pass string from C/C++ DLL to C# code. Like here: https://stackoverflow.com/questions/20752001/passing-strings-from-c-sharp-to-c-dll-and-back-minimal-example – jacekbe Jul 03 '17 at 06:13
0
#include <windows.h>
#include <shellapi.h>

int argc = 0;
auto wargv = CommandLineToArgvW(GetCommandLineW(), &argc);

auto program_path = wargv[0];

...


LocalFree(wargv);

documentation:

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

It depends.

If you are using c++ with /clr you can use read the name of the Process returned from Process::GetCurrentProcess().

In native code in Windows you can use GetModuleFileName()

In Linux or MAC there are different options depending on your platform.

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41