2

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.

Gera
  • 65
  • 5
  • You would need to cast it like in your second example. Does it work if `szModule` is a `const char*` instead? It doesn't look like `GetModuleInfo` needs to modify `szModule`, so it should be safe as a `const char*`. – Tas Sep 25 '15 at 03:25
  • How is using Unicode not very practical? It's the preferred way of doing things in Win32. If you insist on using char* strings in your codebase, you're going to need to make use of conversion functions such as WideCharToMultiByte/MultiByteToWideChar. – gigaplex Sep 25 '15 at 03:28
  • @gigaplex I didn't mean to say it was impractical to use unicode. I meant to say that it was not practical to have a static string in there. What bothers me the most is why setting the project character set to **Use Multi-Byte Character Set** didn't work for me but worked for many other people who were using Visual studio 2012 and less. I want to do that so I don't have to write the WideCharToMultiByte everytime I need to pass a LPCWSTR. – Gera Sep 25 '15 at 03:34
  • If you have access to MFC or ATL, you could just use some wrapper classes like CString or CComBSTR to save the trouble of manually converting. Otherwise you can write your own equivalent class. As for why switching to multi-byte in project settings didn't work for you, it sounds like something is messing with the UNICODE #define. Check your headers and code for that define, and do a full rebuild just in case the precompiled header didn't get rebuilt when you changed the configuration. – gigaplex Sep 25 '15 at 03:57

1 Answers1

2

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

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

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Worked perfectly. Care to elaborate on how it's different? – Gera Sep 25 '15 at 03:29
  • `GetModuleHandle` is a macro which become to either `GetModuleHandleA` or `GetModuleHandleW`. `GetModuleHandleA` explicitly use ASCII (char). – MikeCAT Sep 25 '15 at 03:35