我目前面临以下问题。我已经搜索了任何解决方案,但找不到与我的问题有关的任何有用信息。传递参数时数据类型不匹配
我想使用VBA提供一些参数给一个自己写的DLL,它将为我计算出一个结果。
我的问题:只要我尝试处理传递给函数setCEPCI的值,我就会收到一个“Microsoft Excel已停止工作” - 错误(无更多详细信息)。如果我将这三行(请参阅我的复制代码)置于注释中,但是,一切工作正常。我尝试了很多解决方法,但都归结为这些方面。 令人惊讶的是,我可以在第一个函数getPEC中使用传递的参数,例如, x是我的流出。
对于记录我使用def文件导出的功能,虽然恕我直言,错误似乎是一些数据类型的不匹配。
编辑:澄清:如果找不到我的DLL例如由于RV分配,我得到相同的Microsoft-Excel错误。如果我尝试在第一个函数中为RV分配任何值,我也会得到该错误。另一方面,我可以毫无问题地给PEC分配任何值。
我的DLL的样子:
extern "C" {
typedef void(CALLBACK * FncPtrClssGetCEPCI)(CEPCIvalues &, int, int, int &);
double PEC_backUp;
const int numbMaxCEPCIlistMembers = 3;
CEPCIvalues cepcilist_backUp[numbMaxCEPCIlistMembers];
void __stdcall getPEC(int typeOfPump, double outflow, double &PEC, int &RV)
{
//y = a x^6 + b x^5 + c x^4 + d x^3 + e x^2 + f x^1 + g x^0
if ((typeOfPump < 1) || (10 < typeOfPump)) {
RV = -1;
return;
}
double a, b, c, d, e, f, g;
//...
PEC_backUp = a * pow(outflow, 6.0) +
b * pow(outflow, 5.0) +
c * pow(outflow, 4.0) +
d * pow(outflow, 3.0) +
e * pow(outflow, 2.0) +
f * pow(outflow, 1.0) +
g * pow(outflow, 0.0);
double EUR_USD_07_2000 = 0.939082609;
PEC_backUp = PEC_backUp/EUR_USD_07_2000;
PEC = PEC_backUp;
}
void __stdcall setCEPCI(int monthIN, int yearIN, int &RV)
{
//HINSTANCE hinstCEPCI = LoadLibraryEx("CEPCI.dll", NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
HINSTANCE hinstCEPCI = LoadLibrary("C:\\Users\\...somePath...\\CEPCI.dll");
if (!hinstCEPCI)
{
RV = -11;
return;
}
FncPtrClssGetCEPCI ptrGetCEPCI = (FncPtrClssGetCEPCI)GetProcAddress(hinstCEPCI, "getCEPCI");
if (!ptrGetCEPCI)
{
RV = -22;
return;
}
//CEPCI values of 13/1970 and 07/2000 are automatically extracted
//due to less interaction and, thus, risk of errors in user input
int monthIN_auto = 13;
int yearIN_auto = 1970;
ptrGetCEPCI(cepcilist_backUp[0], monthIN_auto, yearIN_auto, RV);
monthIN_auto = 7;
yearIN_auto = 2000;
ptrGetCEPCI(cepcilist_backUp[1], monthIN_auto, yearIN_auto, RV);
//now extract CEPCI value of user specific input
// monthIN_auto = monthIN;
// yearIN_auto = yearIN;
ptrGetCEPCI(cepcilist_backUp[2], monthIN_auto, yearIN_auto, RV);
CEPCIvalues cepcilist;
cepcilist = cepcilist_backUp[2];
// RV = monthIN + yearIN;
ptrGetCEPCI = 0;
FreeLibrary(hinstCEPCI);
hinstCEPCI = 0;
return;
}
我的VBA代码如下所示:
Public Declare Sub getPEC _
Lib "C:\Users\...somePath...\OPunit0011PUMP.dll" _
(ByVal typeOfPump As Integer, ByVal outflow As Double, ByRef PEC As Double, ByRef RV As Integer)
Public Declare Sub setCEPCI _
Lib "C:\Users\...somePath...\OPunit0011PUMP.dll" _
(ByVal monthIN As Integer, ByVal yearIN As Integer, ByRef RV As Integer)
Function CallPump()
Dim typeOfPump, RV, monthIN, yearIN As Integer
typeOfPump = 9
Dim outlfow, PEC As Double
outflow = 100
monthIN = 5
yearIN = 2008
Call getPEC(typeOfPump, outflow, PEC, RV)
Call setCEPCI(monthIN, yearIN, RV)
End Function
任何帮助表示赞赏。先谢谢你。
您是否尝试过将Declare更改为... Declare PtrSafe Sub。 VBA7需要Prtsafe – MiguelH
没有工作... – PublicUserName