有没有办法加载32位DLL库(具有与LoadLibrary相同的用法)我想使用该函数和GetProcAddress.
我看了WOW,但它似乎没有提供功能.功能应该存在,因为像DependencyWalker这样的工具能够读取32位dll的符号,即使它的64位也是如此.
谢谢
大家好:我已经在我的项目中加载了我的DLL,但每当我使用GetProcAddress函数时.它返回NULL!我该怎么办?我在"MYDLL.dll"中使用此函数(双GetNumber(double x))
这是我使用的代码:
typedef double (*LPGETNUMBER)(double Nbr);
HINSTANCE hDLL = NULL;
LPGETNUMBER lpGetNumber;
hDLL = LoadLibrary(L"MYDLL.DLL");
lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");
Run Code Online (Sandbox Code Playgroud) 出于某种原因,每当我的C#.NET 2.0应用程序调用GetProcAddress
它时,总是返回零.
public class MyClass
{
internal static class UnsafeNativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetDllDirectory(string lpPathName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
}
private void MyFunc()
{
IntPtr _dllHandle;
IntPtr _fptr;
string _fullPath = ".\\mydll.dll";
string _procName = "MyDllFunc";
_dllHandle = UnsafeNativeMethods.LoadLibrary(_fullPath);
_fptr = UnsafeNativeMethods.GetProcAddress(_dllHandle, _procName); // <-- …
Run Code Online (Sandbox Code Playgroud) 我用一个名为"render()"的函数创建了一个DLL,我想将它动态加载到我的应用程序中,但是GetProcAddress无法找到它.这是DLL .h:
#ifdef D3D_API_EXPORTS
#define D3D_API_API __declspec(dllexport)
#else
#define D3D_API_API __declspec(dllimport)
#endif
D3D_API_API void render();
Run Code Online (Sandbox Code Playgroud)
这是DLL .cpp:
#include "stdafx.h"
#include "D3D_API.h"
#include <iostream>
D3D_API_API void render()
{
std::cout << "method called." << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这是尝试使用该功能的应用程序:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE myDLL = LoadLibrary( L"D3D_API.dll" );
if (myDLL == NULL) {
std::cerr << "Loading of D3D_API.dll failed!" << std::endl;
}
typedef void (WINAPI *render_t)();
render_t render = (render_t)GetProcAddress( myDLL, "render" );
if (render …
Run Code Online (Sandbox Code Playgroud) 首先,我知道直接比较dllimport属性和getProcAddress函数是没有意义的.相反,我感兴趣的是比较两段代码,通过使用dllimport属性或使用getProcAddress函数导入函数,实现基本相同的事情 - 在dll中调用函数.具体来说,我正在编写一个C#应用程序,它在我编写的dll中使用了一些函数.起初我使用以下代码访问了我的dll函数:
class DllAccess
{
[DllImport("kernel32.dll", SetLastError = true)]
private extern IntPtr LoadLibrary(String DllName);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate Bool BarType(Byte arg); // return value indicates whether function call went well or not.
Bool Bar(Byte arg)
{
Bool ok = false;
IntPtr pDll= LoadLibrary("foo.dll");
if (pDll != IntPtr.Zero)
{
IntPtr pfunc = GetProcAddress(pDll, "bar");
if (pFunc != IntPtr.Zero)
{
BarType bar = (BarType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(BarType));
ok = bar(arg);
}
FreeLibrary(pDll);
}
return ok;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我后来需要获取lastError值,如果它已在dll调用期间设置,那么我将我的代码更改为:
class DllAccess
{
[DllImport("foo.dll", EntryPoint = …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个 MFC 项目,该项目尝试调用 DLL 中的函数,该函数将以字符串形式返回一些信息。DLL中的函数如下:
int GetInfo(char* Info)
Run Code Online (Sandbox Code Playgroud)
如果成功,该函数将返回 0。信息将在字符串参数中返回。调用例程如下:
typedef int (WINAPI *FUNC1)(char* szInfo);
HINSTANCE hinstLib;
FUNC1 GetInfo;
char szInfo[50];
hinstLib = LoadLibrary(TEXT("DevInfo.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
GetInfo = (FUNC1) GetProcAddress(hinstLib, "GetInfo");
// If the function address is valid, call the function.
if (NULL != GetInfo)
{
if((GetInfo) (szInfo)) // Error here!!
{
AfxMessageBox(_T("Error Reading"));
}
}
FreeLibrary(hinstLib);
}
Run Code Online (Sandbox Code Playgroud)
此代码在编译和链接时没有错误。执行时,会在上述位置返回“访问冲突执行位置0x00000000”的错误。任何人都可以建议吗?
首先,是的,我已经搜索了一段时间,但找不到与我的案例相关的任何答案。基本上,我试图在 Windows DLL (dnsapi.dll) 中获取函数的地址,而 GetProcAddress 返回 0。经过一段时间的挠头,我什至继续创建一个程序,该程序仅在 user32 中使用 MessageBox 的 GetProcAddress。 dll。结果一样。这是我制作的第二个程序的代码,它仍然不起作用:
#include <stdio.h>
#include <Windows.h>
int main() {
HINSTANCE hLib = LoadLibrary(TEXT("user32.dll"));
DWORD MsgBoxAddr = (DWORD)GetProcAddress(hLib, "MessageBox");
printf("%ld", MsgBoxAddr);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,GetLastError 返回 127,这似乎是 GetProcAddress 不起作用时返回的最常见错误,但我不知道出了什么问题,我尝试了许多函数和 DLL,而不仅仅是这对夫妇。
谢谢。:)
编辑:链接的文章解决了我的问题,我尝试过的所有功能都有 unicode 和 anis 版本(w 和 a)。使用完整的 API 名称解决了这些问题。感谢您链接该问题。
谢谢你。
这是我的 DLL 代码:
#include <Windows.h>
#include <iostream>
int sysLol(char *arg);
int sysLol(char *arg)
{
std::cout<<arg<<"\n";
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序代码:
#include <Windows.h>
#include <iostream>
#include <TlHelp32.h>
#include <stdlib.h>
typedef int (WINAPI* Lol)(char* argv);
struct PARAMETERS
{
DWORD Lol;
};
int main()
{
PARAMETERS testData;
HMODULE e = LoadLibrary(L"LIB.dll"); //This executes without problem
if (!e) std::cout<<"LOADLIBRARY: "<<GetLastError()<<"\n";
else std::cout<<"LOADLIBRARY: "<<e<<"\n";
testData.Lol = (DWORD)GetProcAddress(e,"sysLol"); //Error 127?
if (!testData.Lol) std::cout<<testData.Lol<<" "<<GetLastError()<<"\n";
else std::cout<<"MESSAGEBOX: "<<testData.Lol<<"\n";
std::cin.ignore();
return 1;
}
Run Code Online (Sandbox Code Playgroud)
所以,我的 LIB.dll 使用 成功加载LoadLibrary()
,但 …
我想以这种方式调用 MessageBox() 函数:
1)。加载所需的库
2)。获取函数地址
3). 叫它
因此,为了我所理解的目标,我应该在 MessageBox 函数中使用所有类型的参数定义新类型。
它返回 INT 并接受:HWND、LPCSTR、LPCSTR、UNIT。
所以我注册了新类型:
typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);
Run Code Online (Sandbox Code Playgroud)
我在调用此类函数时遇到问题。这种方式适用于所有功能还是仅适用于导出?
我怎样才能准确地以这种方式调用MessageBox?
完整代码:
#include <iostream>
#include <windows.h>
using namespace std;
typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);
int main(void)
{
HINSTANCE__ *hModule = LoadLibrary(L"\\Windows\\System32\\User32.dll");
msgbox *me = 0;
if(hModule != 0)
{
me = (msgbox*)GetProcAddress(hModule, "MessageBox");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
//mydll.cpp
#include <Windows.h>
#include <io.h>
#define STDOUT_FILEDESC 1
class MYSTDOUT {
bool shouldClose;
bool isBuffered;
public:
MYSTDOUT(bool buf = true, bool cl = true)
: isBuffered(buf),
shouldClose(cl)
{}
~MYSTDOUT() {
if (shouldClose) {
close(STDOUT_FILEDESC);
}
}
};
__declspec(dllexport) void* mydll_init_stdout()
{
static MYSTDOUT outs;
return &outs;
}
//test_dll.cpp
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <io.h>
typedef void* (__cdecl *MYPROC)(void);
int main(void)
{
int fd;
void *pstdout;
MYPROC init_stdout;
HMODULE handle = LoadLibrary(TEXT("mydll.dll"));
init_stdout = (MYPROC)GetProcAddress(handle,"mydll_init_stdout");//NULL
FreeLibrary((HMODULE) handle);
return …
Run Code Online (Sandbox Code Playgroud)