1

C怎么调用dll里的所有函数

 2 years ago
source link: https://www.oschina.net/question/5094283_2324877
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C怎么调用dll里的所有函数

红军24 发布于 01/09 22:11
阅读 235

            //然后查找dll,调用该函数。
            HINSTANCE h = LoadLibraryA("msvcrt.dll");  

typedef int (* Fun_exit)(int a);

if  (h == NULL)  {
                FreeLibrary(h);  
                printf("load lib error\n");
            } else {
                Fun_exit funPtr = (Fun_exit)GetProcAddress(h, “exit”);
                if(funPtr != NULL) {
                    funPtr(0);
                } else {
                    printf("get procedure error\n");
                    printf("%d",GetLastError());
                }
                FreeLibrary(h);
            }

       以上,调用了msvcrt.dll里的exit函数 -exit是一个C标准库函数。         GetProcAddress的返回结果是void *,我们必须把这个指针转化成正确的函数指针类型。对于exit函数,转化成Fun_exit;对于strlen函数,转换成Fun_strlen类型.....

      msvcrt.dll里有上百个函数,每个函数原型都不一样,当然,我可以查资料,每一个都写typedef出来:

            typedef int (* Fun_exit)(int a);
            typedef int (* Fun_strlen)(char *p);
.......

     if  (proc == "exit")

            Fun_exit  p = (Fun_exit *) (...)

     if  (proc == "strlen")

            Fun_strlen  p =  (Fun_strlen *) (...)

     .......

        除此之外,还有更方便的写法吗?因为,单单就一个msvcrt.dll文件有上百个函数处理,而我要调用的dll文件很多呢,简单说来,只要是个dll,都希望能调用。怎么写这一部分代码?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK