c++unicodewchar-tchar-pointerlpcwstr

How to pass a char* to the GetModuleHandle function?


I'm just trying to get the module information based on a string that can very well be something like "somefile.exe".

MODULEINFO GetModuleInfo(char *szModule)
{
    MODULEINFO modinfo = {0};
    HMODULE hModule = GetModuleHandle(szModule);
    if(hModule == 0) 
       return modinfo;
    GetModuleInformation(GetCurrentProcess(), hModule, &modinfo, sizeof(MODULEINFO));
    return modinfo;
}

The error argument of type "char *" is incompatible with parameter of type "LPCWSTR" appears in GetModuleHandle.

Now, I've tried numerous steps such as:

  1. Going to the Project Properties → Configuration Properties → Character Set and setting it to Use Multi-Byte Character Set rather than Unicode. I've seen it help other people, but it did not do the trick for me.

  2. Casting the szModule like (LPCWSTR)szModule. This made the program build successfully, however it didn't work as intended.

  3. Using unicode by replacing the parameter with a static L"somefile.exe". This worked perfectly, however, needless to say it's not very practical.

My question is: How can I make GetModuleHandle(szModule) compile if szModule is of type char*?

I'm using Visual Studio 2015 if it helps anything.


Solution

  • I don't think making GetModuleHandle(szModule) have the future is a good idea.

    Try using GetModuleHandleA(szModule) instead for char *szModule;.