Vanara icon indicating copy to clipboard operation
Vanara copied to clipboard

Kernel32 GetProcAddress for 64its

Open iR3SH opened this issue 1 year ago • 9 comments

Hi, I'm trying to use the function GetProcAddress, when i run my application in 32Bits everthing works, but not when i launch it in Any CPU or 64Bits

Could you make the method compatible with 64Bits ?

Thanks :)

iR3SH avatar Aug 05 '24 21:08 iR3SH

Which overload are you using and what exception or result code are you getting?

dahall avatar Aug 06 '24 00:08 dahall

I found something, maybe this is helpful:

StackOverflow: GetProcAddress function in C++

GetProcAddress function in C++

lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");

Checking return codes and calling GetLastError() will set you free. You should be checking return codes twice here. You are actually checking return codes zero times.

hDLL = LoadLibrary(L"MYDLL.DLL");

Check hDLL. Is it NULL? If so, call GetLastError() to find out why. It may be as simple as "File Not Found".

lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");

If lpGetNumber is NULL, call GetLastError(). It will tell you why the proc address could not be found. There are a few likely scenarios:

tajbender avatar Aug 06 '24 04:08 tajbender

Another Hint: https://stackoverflow.com/questions/10250323/getprocaddress-null-is-returned?rq=3

tajbender avatar Aug 06 '24 06:08 tajbender

Thanks for all of your answer, i use it in a C# Library project, So first i Load my Library like this : SafeHINSTANCE safeModule = LoadLibraryEx(DllUrl); Then i want to load the functions of the dll like this GetProcAddress(safeModule, "CCAPIConnectConsole") But then i look if any load of functions are loaded correclty

safeModule = LoadLibrary(DllUrl);
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIConnectConsole"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIDisconnectConsole"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetConnectionStatus"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetBootConsoleIds"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetConsoleIds"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetMemory"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetMemory"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetProcessList"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetProcessName"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetTemperature"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIShutdown"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIRingBuzzer"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetConsoleLed"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetFirmwareInfo"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIVshNotify"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetNumberOfConsoles"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetConsoleInfo"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetDllVersion"));

private bool IsCCAPILoaded()
        {
            for (int i = 0; i < CCAPIFunctionsList.Count; i++)
                if (CCAPIFunctionsList.ElementAt(i) == IntPtr.Zero)
                    return false;
            return true;
        }

And if i call GetLastError() i got the error code 0, in 64Bits i've an message who told me that the function is not found but in 32Bits it works well, so Every element on my list in 64Bits = IntPtr.Zero

iR3SH avatar Aug 06 '24 08:08 iR3SH

Please try this:https://csharp.hotexamples.com/fr/examples/-/getDllVersionDelegate/-/php-getdllversiondelegate-class-examples.htmlAm 06.08.2024 10:49 schrieb iR3SH @.***>: Thanks for all of your answer, i use it in a C# Library project, So first i Load my Library like this : SafeHINSTANCE safeModule = LoadLibraryEx(DllUrl); Then i want to load the functions of the dll like this GetProcAddress(safeModule, "CCAPIConnectConsole") But then i look if any load of functions are loaded correclty safeModule = LoadLibrary(DllUrl); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIConnectConsole")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIDisconnectConsole")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetConnectionStatus")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetBootConsoleIds")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetConsoleIds")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetMemory")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetMemory")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetProcessList")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetProcessName")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetTemperature")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIShutdown")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIRingBuzzer")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetConsoleLed")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetFirmwareInfo")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIVshNotify")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetNumberOfConsoles")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetConsoleInfo")); CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetDllVersion")); private bool IsCCAPILoaded() { for (int i = 0; i < CCAPIFunctionsList.Count; i++) if (CCAPIFunctionsList.ElementAt(i) == IntPtr.Zero) return false; return true; }

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

tajbender avatar Aug 06 '24 10:08 tajbender

This was my bad, i'm tryng to load an 32Bits Library in an 64Bits tool, it can't work i think

iR3SH avatar Aug 06 '24 10:08 iR3SH

This was my bad, i'm tryng to load an 32Bits Library in an 64Bits tool, it can't work i think

https://stackoverflow.com/questions/2265023/load-32-bit-dll-library-in-64-bit-application

dahall avatar Aug 06 '24 20:08 dahall

Also, for functions like this that use a NULL handle to report failure, you can use Win32Error.ThrowLastErrorIfInvalid

CCAPIFunctionsList.Add(Win32Error.ThrowLastErrorIfInvalid(GetProcAddress(safeModule, "CCAPIGetDllVersion")));

dahall avatar Aug 06 '24 20:08 dahall

Also, for functions like this that use a NULL handle to report failure, you can use Win32Error.ThrowLastErrorIfInvalid

CCAPIFunctionsList.Add(Win32Error.ThrowLastErrorIfInvalid(GetProcAddress(safeModule, "CCAPIGetDllVersion")));

@dahall: Another unrelated Question:

Does this always work?

Or does this only work on specific DLLs, like Shel32, Kernel32 etc.?

edit: How do I know, my specific call is supported by this approach?

tajbender avatar Aug 07 '24 05:08 tajbender

Any handle in Vanara that supports the IHandle interface or any IntPtr value will work with the Win32Error.ThrowLastErrorIfInvalid method.

dahall avatar Sep 04 '24 16:09 dahall