dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.81k stars 196 forks source link

Kernel32 GetProcAddress for 64its #478

Closed iR3SH closed 2 months ago

iR3SH commented 3 months ago

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 :)

dahall commented 3 months ago

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

tajbender commented 3 months ago

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 commented 3 months ago

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

iR3SH commented 3 months ago

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

tajbender commented 3 months ago

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: @.***>

iR3SH commented 3 months ago

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

dahall commented 3 months ago

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 commented 3 months ago

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")));
tajbender commented 3 months ago

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?

dahall commented 2 months ago

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