尝试从dll应用程序访问返回类型的DLL函数时出现编译器错误? [英] Getting Compiler error while trying to access return type of dll function from dll application?
本文介绍了尝试从dll应用程序访问返回类型的DLL函数时出现编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个想要访问返回类型的dll函数的Dll应用程序,返回类型是'char **'
这是我的Dll应用程序
I have a Dll application which wants to access return type of dll function,the return type is 'char **'
This is my Dll application
#include <windows.h>
#include <stdio.h>
typedef int (__cdecl *MYPROC)(LPWSTR);
int main( void )
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
static int Counter = 0;
char **Arr = (char **)malloc(sizeof(char*) * 10);
for(;Counter<8;Counter++)
{
Arr[Counter] = (char *) malloc(256);
}
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("ConfigurationDll.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "InAgentConfReader");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (L"Message sent to the DLL function\n");
}
// StringReturn returns a (char **)
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "StringReturn");
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
//problem is here
// error C2440: '=' : cannot convert from 'int' to 'char **'
Arr = (ProcAdd) (L"Message sent to the DLL function\n");
//when i remove Assingment of ProcAdd into Arr it works fine
printf("%s",Arr[0]);
}
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
return 0;
}
我想在应用程序中使用我的应用程序的局部变量从'StringReturn'打印返回,告诉我一个方法这样做吗?
除了我在这个应用程序本身的控制台调用dll函数上打印文本时,dll没有任何问题
谢谢
I want to a print return from 'StringReturn' in the application using local variable of my application only,tell me a way to do it??
Apart there is not any problem in dll as i printed text on console calling dll function from this application itself
thanks
推荐答案
您已声明MYPROC
返回int
结果,但是在你的代码中,你试图将结果存储到char **
,这是不匹配的。您还在数组中分配空间,该空间将被库调用的返回值覆盖。它可能是一个更好的设计,将数组地址发送到库函数并允许它填写细节,并返回一些表示成功或失败的值。
You have declaredMYPROC
as returning anint
result, but then in your code you are trying to store that result to achar**
, which does not match. You are also allocating space in the array which will be overwritten by the return value from the library call. It would probably be a better design, to send the array address to the library function and allow it to fill in the details, and return some value which indicates success or failure.
// change your declaration so it matches the function:
typedef char** (__cdecl *MYPROC)(LPWSTR);
这篇关于尝试从dll应用程序访问返回类型的DLL函数时出现编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文