【发布时间】:2010-06-21 03:30:43
【问题描述】:
从这个例子开始:http://support.microsoft.com/kb/828736 我试图在我的 C# DLL 中添加一个测试函数,它将字符串作为参数。我的 C# DLL 代码如下:
namespace CSharpEmailSender
{
// Interface declaration.
public interface ICalculator
{
int Add(int Number1, int Number2);
int StringTest(string test1, string test2);
};
// Interface implementation.
public class ManagedClass : ICalculator
{
public int Add(int Number1, int Number2)
{
return Number1 + Number2;
}
public int StringTest(string test1, string test2)
{
if (test1 == "hello")
return(1);
if (test2 == "world")
return(2);
return(3);
}
}
然后我使用 regasm 注册这个 DLL。我在我的 C++ 应用程序中使用它,如下所示:
using namespace CSharpEmailSender;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
ICalculatorPtr pICalc(__uuidof(ManagedClass));
long lResult = 0;
long lResult2 = 0;
pICalc->Add(115, 110, &lResult);
wprintf(L"The result is %d", lResult);
pICalc->StringTest(L"hello", L"world", &lResult2);
wprintf(L"The result is %d", lResult2);
// Uninitialize COM.
CoUninitialize();
return 0;
}
运行后,lResult 是正确的(值为 225),但 lResult2 为零。知道我做错了什么吗?
【问题讨论】:
-
您是否尝试过调试程序并在
StringTest函数中设置断点?你能检查一下传入的字符串的值吗?如果是,它们是什么?