在dll中调用dll

时间:2018-02-09 20:50:00

标签: c dll console-application

我会尽力保持这个问题的广泛性,欢迎任何见解。

所以,我有2个场景。

场景#1 我有一个C,Win 32控制台应用程序,控制台应用程序执行以下操作:

(1) opens a file and writes to it.
(2) loads and calls a dll function, sends some input/ receives some output.
(3) freeLibrary(dllLibrary)
(4) opens a file and writes to it.

在场景#1中,一切都很好。

场景#2 我有一个C,Win 32控制台应用程序,控制台应用程序执行以下操作:

(1) opens a file and writes to it.
(2) loads and calls a new dll function, sends some input/ receives some output.
(3) the new dll function calls the dll function in scenario #1 and then calls freeLibrary(dllLibrary).
(4) once the new dll function returns back to the console application freeLibrary(newdllLibrary) is called.
(5) opens a file and writes to it. ==> ERROR

因此,在方案#2的步骤(5)中,我收到一个未处理的异常,访问冲突错误。经过研究,似乎问题可能是由于指针损坏。我没有在新的dll中使用malloc,它实际上与场景#1中的控制台应用程序具有相同的调用代码。我没有原始dll的原始源代码,因此调试这可能甚至不可能,但我希望能够通过思考这个问题。

以下是我的问题/疑虑/事实,

(1) I can call the new dll and if the new dll does NOT call the original dll, everything works fine. 
(2) Is the original dll corrupting data in scenario #1, and I just haven't run into an Unhandled Exception yet? 
(3) Is it possible that data is becoming corrupted due to an exe => new dll => original dll. In other words, do I need to do anything special in the new dll source code to preserve the console application's variables.

在方案#2中,返回的数据是正确的。如果我尝试打开文件,甚至调用printf等基本printf语句(" Hello World"),我会收到访问冲突异常。

欢迎所有帮助,批评和建议。我是SOL调试此问题因为我没有原始dll的源代码吗?

编辑:

场景#1代码。 (输入/输出结构是全局定义的,但我将它们排除在外)

typedef UINT (CALLBACK* LPFNDLLFUNCLOOKUP)(AccuInput*, AccuOut*);
LPFNDLLFUNCLOOKUP lpfnDllFuncCASSLookup; 
typedef UINT (CALLBACK* LPFNDLLFUNCINIT)(BSTR); 
LPFNDLLFUNCINIT lpfnDllFuncInit;
typedef UINT (CALLBACK* LPFNDLLFUNCCLOSE)(); 
LPFNDLLFUNCCLOSE lpfnDllFuncClose;
HMODULE unmanagedLib;

int main() {
    BSTR configFile;
    FILE *fp;
    AccuInput accIn = 
    {
        "501 Willow", "", "", "Greenville", "AL", "36037", ""
    };
    AccuOut accOut = {0};

    // Load Library
    unmanagedLib = LoadLibraryA((LPCSTR) "AccuAddressUnMgd.dll");
    lpfnDllFuncInit = (LPFNDLLFUNCINIT)GetProcAddress(unmanagedLib, (LPCSTR)"Init");
    lpfnDllFuncCASSLookup = (LPFNDLLFUNCLOOKUP)GetProcAddress(unmanagedLib, (LPCSTR)"AccuCassLookup");
    lpfnDllFuncClose = (LPFNDLLFUNCCLOSE)GetProcAddress(unmanagedLib, (LPCSTR)"Close");
    configFile = SysAllocString(L"C:\\Program Files (x86)\\AccuZIPToolkit\\Config.acu");
    lpfnDllFuncInit(configFile); 
    SysFreeString(configFile);

    // write input to file
    fp = fopen ("C:pathToFile.txt","a");
    fprintf (fp, "\n------- TEST -------\n");
    fclose(fp);

    // lookup address
    lpfnDllFuncCASSLookup(&accIn,&accOut);

    // write output to file
    fp = fopen ("C:pathToFile.txt","a");
    fprintf (fp, "\n------- TEST -------\n");
    fclose(fp);

    // close function
    lpfnDllFuncClose();

    FreeLibrary(unmanagedLib);

    return 0;
}

情景#2

控制台应用主要功能

typedef void (CALLBACK* LPFNDLLFUNC1)(char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,char*);

int main() {
HMODULE hDLL;
LPFNDLLFUNC1 lpfnDllFunc; 
FILE* fp;

char name[35+1] = "";
char addr1[35+1] = "501 Willow";
char addr2[35+1] = "";
char city[20+1] = "Greenville";
char state[2+1] = "AL";
char zip[10+1] = "36037";
char rdi[1+1] = "";
char returnCode[2+1] = "";
char returnText[101] = "";
char errorWarningCode[2+1] = "";
char errorWarningText[100+1] = "";

// write to file
fp = fopen ("C:PathTo.txt","a");
fprintf (fp, "INPUT\n");
fclose(fp);

// call new dll
hDLL = LoadLibraryA((LPCSTR)"ValidateRDI.dll");
lpfnDllFunc = (LPFNDLLFUNC1)GetProcAddress(hDLL,"ValidateAddress");
lpfnDllFunc(name,addr1,addr2,city,state,zip,rdi,returnCode,returnText,errorWarningCode,errorWarningText);

// write to file
fp = fopen ("C:PathTo.txt","a");
fprintf (fp, "OUTPUT\n");
fclose(fp);

FreeLibrary(hDLL);

getchar();

return 0;
}

新的DLL函数源

typedef UINT (CALLBACK* LPFNDLLFUNCLOOKUP)(AccuInput*, AccuOut*);
LPFNDLLFUNCLOOKUP lpfnDllFuncCASSLookup; 
typedef UINT (CALLBACK* LPFNDLLFUNCINIT)(BSTR); 
LPFNDLLFUNCINIT lpfnDllFuncInit;
typedef UINT (CALLBACK* LPFNDLLFUNCCLOSE)(); 
LPFNDLLFUNCCLOSE lpfnDllFuncClose;
HMODULE unmanagedLib;
void CSIEXPORT ValidateAddress(char *Name,
                                         char *Addr1,
                                         char *Addr2,
                                         char *City,
                                         char *State,
                                         char *Zip,
                                         char *RDI,
                                         char *ReturnCode,
                                         char *ReturnText,
                                         char *ErrorWarningCode,
                                         char *ErrorWarningText)
{
BSTR configFile; // must be first line of function
AccuInput accIn = {0};
AccuOut accOut = {0};
Name[35] = '\0';
Addr1[35] = '\0';
Addr2[35] = '\0';
City[20] = '\0';
State[2] = '\0';
Zip[10] = '\0';
RDI[01] = '\0';

memset(ReturnCode, ' ', sizeof(ReturnCode));
memset(ReturnText, ' ', sizeof(ReturnText));
memset(ErrorWarningCode, ' ', sizeof(ErrorWarningCode));
memset(ErrorWarningText, ' ', sizeof(ErrorWarningText));
ReturnCode[2] = '\0';
ReturnText[100] = '\0';
ErrorWarningCode[2] = '\0';
ErrorWarningText[100] ='\0';
strncpy(accIn.AddressLine1, Addr1, sizeof(accIn.AddressLine1));
accIn.AddressLine1[sizeof(accIn.AddressLine1)-1] = '\0';
strncpy(accIn.AddressLine2, Addr2, sizeof(accIn.AddressLine2));
accIn.AddressLine2[sizeof(accIn.AddressLine2)-1] = '\0';
strncpy(accIn.Company, Name, sizeof(accIn.Company));
accIn.Company[sizeof(accIn.Company)-1] = '\0';
strncpy(accIn.City, City, sizeof(accIn.City));
accIn.City[sizeof(accIn.City)-1] = '\0';
strncpy(accIn.State, State, sizeof(accIn.State));
accIn.State[sizeof(accIn.State)-1] = '\0';
strncpy(accIn.zipcode, Zip, sizeof(accIn.zipcode));
accIn.zipcode[sizeof(accIn.zipcode)-1] = '\0';
strncpy(accIn.Urban, "", sizeof(accIn.Urban));
accIn.Urban[sizeof(accIn.Urban)-1] = '\0';

unmanagedLib = LoadLibraryA((LPCSTR) "AccuAddressUnMgd.dll");
lpfnDllFuncInit = (LPFNDLLFUNCINIT)GetProcAddress(unmanagedLib, (LPCSTR)"Init");
lpfnDllFuncClose = (LPFNDLLFUNCCLOSE)GetProcAddress(unmanagedLib, (LPCSTR)"Close");
configFile = SysAllocString(L"C:\\Program Files (x86)\\AccuZIPToolkit\\Config.acu");
lpfnDllFuncInit(configFile);
SysFreeString(configFile);
lpfnDllFuncCASSLookup(&accIn,&accOut);
lpfnDllFuncClose();
FreeLibrary(unmanagedLib);
return;
}

0 个答案:

没有答案