2015-09-02 28 views
0

我目前面临以下问题。我已经搜索了任何解决方案,但找不到与我的问题有关的任何有用信息。传递参数时数据类型不匹配

我想使用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 

任何帮助表示赞赏。先谢谢你。

+0

您是否尝试过将Declare更改为... Declare PtrSafe Sub。 VBA7需要Prtsafe – MiguelH

+0

没有工作... – PublicUserName

回答

0

我想我解决了我的问题。

望着VBA代码

Dim typeOfPump, RV, monthIN, yearIN as Integer 

只宣布任何理由typeOfPump为整数,其余仍变种(与实施看着Excel函数监控时)。写作

Dim typeOfPump as Integer 
Dim RV as Integer 
... 

似乎解决了这个问题。不幸的是,我无法解释第一行有什么问题。

+0

它是VBA的一种弱点。如果用逗号分隔其他项目,只有第一个Dim设置为数据类型。其他项目变成变体。 – MiguelH

+0

我之前明确地寻找过。在这里看到: https://msdn.microsoft.com/en-us/library/7ee5a7s1.aspx **定义多个变量 你可以在一个声明语句中声明几个变量,为每一个指定的变量名称,并按照每个数组名都带括号。多变量之间用逗号隔开。** 这似乎很奇怪我。 – PublicUserName

+0

这是指Visual Basic。 Excel的VBA更是一个Visual Basic精简版的! – MiguelH