导出c++函数,在vb6中声明,得到“远程过程调用失败";运行时错误 [英] Exporting c++ function, declared in vb6, getting "remote procedure call failed" runtime error

查看:49
本文介绍了导出c++函数,在vb6中声明,得到“远程过程调用失败";运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C++ 编写的 dll,正在像这样导出:

I have a dll written in C++ that is being exported like so:

extern "C" {
    extern _declspec(dllexport) (VOID) Merge(LPCSTR szFileName, LPCSTR szMergeString, DWORD dwAlignment, BOOL fRepeat);
}

该函数在别处定义为:

extern _declspec(dllexport) (VOID) Merge(LPCSTR szFileName, LPCSTR szMergeString, DWORD dwAlignment, BOOL fRepeat)
{   
    Tools::Merge(gcnew String(szFileName), gcnew String(szMergeString), dwAlignment, fRepeat == TRUE);
}

在我的 vb6 dll 中,我像这样声明了远程调用:

In my vb6 dll, I have declared the remote call like this:

Private Declare Sub Merge Lib "Tools.dll" (fileName As String, mergeString As String, Alignment As Long, repeated As Boolean)

然后我在代码中这样调用它:

Then I call it in the code like this:

Merge strPageFileName, "COPY", 5, True

当我运行应用程序时,我在消息框中收到自动化错误:远程过程调用失败".

When I run the app, I get "Automation error: the remote procedure call failed" in a message box.

Tools.dll 位于 C:\Windows\system32.我用一个错误的 dll 名称进行了测试,以确保这不是问题,并得到一个错误,说它找不到文件,所以我知道不是这样.我还需要做些什么才能让它发挥作用?

Tools.dll is in C:\Windows\system32. I tested with a bad dll name, to make sure that wasn't the problem, and get an error saying it could not find the file, so I know that's not it. Anything else I need to do to get this to work?

根据下面的 xxbbcc 答案更改变量类型可以使其正常运行.然而,它创造了一个新的、迄今为止完全无法解决的问题.我创建了另一个问题here,其中包含一些有关它的详细信息.

Changing the variable type per xxbbcc's answer below allowed this to function correctly. However, it created a new, so far utterly unsolvable problem. I created another question here that has some of the details about it.

远程调用发生在 COM+ dll 中,在一个将 integer 类型返回到另一个非 COM dll 的函数内.出于某种奇怪的原因,COM+ dll 的函数现在返回 VT_ERROR 类型的变体而不是整数.我已经对此进行了彻底的测试,并确定我事先对返回值做什么,将其设置为任何值等都没有关系,它总是返回错误.奇怪的是,错误的值显然是 0.我调用了 CInt(returnValue)CStr(returnValue)CLng(returnValue),它们分别返回 0Error 00.

The remote call is happening in a COM+ dll, inside a function that returns type integer to another non-COM dll. For some strange reason, the COM+ dll's function is now returning a variant of type VT_ERROR instead of an integer. I have tested this thoroughly and determined that it does not matter what I do with the return value beforehand, setting it to any value, etc, it always returns an error. The odd part is that the value of the error is apparently 0. I have called CInt(returnValue), CStr(returnValue), and CLng(returnValue), and they return 0, Error 0, and 0, respectively.

我找不到任何信息来解释为什么应该返回整数的函数会突然返回错误.这在非 COM+ 函数中给了我一个类型不匹配错误,因为它试图将返回值分配给本地整数.将其更改为变体允许我进行上述转换.调用 VarType(returnValue) 给出 10,即 vbError.我们不希望从 COM+ dll 中的其他调用中丢失潜在的失败信息,因此我不能忽略返回值.

I have been unable to find any information explaining why a function that is supposed to return an integer would suddenly be returning an error instead. This is giving me a type mismatch error in the non-COM+ function, as it tries to assign the return to a local integer. Changing that to a variant allowed me to do the casts above. Calling VarType(returnValue) gives 10, which is vbError. We don't want to potentially lose failure information from other calls inside the COM+ dll, so I can't just ignore the return value.

推荐答案

我目前的解决方案是编写一个微型 C++ 控制台应用程序,该应用程序调用导出的函数,并从 vb6 dll 中调用 exe.

My current solution to this has been to write a tiny c++ console application that calls the exported function, and call the exe from within the vb6 dll.

这篇关于导出c++函数,在vb6中声明,得到“远程过程调用失败";运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆