【发布时间】:2014-05-31 01:50:56
【问题描述】:
好的,伙计们,这是我上一篇文章的一个扩展,它已经解决,这部分工作得很好(下面的链接)
Not finding function using GetProcAddress() C++ VBexpress 13
不幸的是,另一个误解出现了。下面是我要参考的代码:
#include "stdafx.h"
#include <iostream>
#include "Windows.h"
#include <stdio.h>
typedef int(__cdecl *MYPROC)(LPWSTR);
using namespace std;
int main()
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
hinstLib = LoadLibrary(TEXT("testDLL.dll"));
if (hinstLib != NULL)
{
ProcAdd = (MYPROC)GetProcAddress(hinstLib, "?Add@MyMathFuncs@MathFuncs@@SANNN@Z");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
c=(ProcAdd)(L"something here");
}
fFreeResult = FreeLibrary(hinstLib);
}
return 0;
}
问题:与函数交互时遇到问题。程序识别 DLL 和函数。我确信它与 typedef、对 ProcAdd 的分配以及我对函数的实际调用有关。在这个例子中,我正在调用一个将双打加在一起的函数。显然我需要通过 2 双打。似乎逻辑决定我可以用'typedef int(__cdecl *MYPROC)(double,double);'替换typedef或类似的东西,用 2 个双精度替换 L"something here" 并将其分配给一个值。当我这样做时没有运行时错误,但我只是为返回的数字提供了一个很大的负数。这两条线到底发生了什么?不幸的是,我什至不确定具体要做什么。我明白 _cdecl 是什么。
简短背景:我必须与一个我没有 .lib 文件的 DLL 接口。我遇到了麻烦,所以我用http://msdn.microsoft.com/en-us/library/ms235636.aspx 的 MS 教程制作了一个 DLL,并用上面的代码引用了那个 DLL,如果我没记错的话,它取自另一个 MS 教程。
对于理解这些基本概念的任何帮助将不胜感激。谢谢!
【问题讨论】:
-
Obviously i need to pass 2 doubles. it seems like logic would dictate i could replace the typedef with 'typedef int(__cdecl *MYPROC)(double,double);' or something similar and replace the L"something here" with 2 doubles and assign it to a value。看起来确实如此。为什么不这样做?What exactly are happening in these 2 lines that are throwing me?未定义的行为正在发生。