我正在编写一个需要调用用C#编写的DLL的C ++程序。我按照这些说明创建了我的C#DLL并从我的C ++链接到它。
https://support.microsoft.com/en-us/kb/828736
我有一个C#函数,它接受一个字符串作为参数。如何将C ++中的字符串传递给我的C#?
答案 0 :(得分:2)
我无法找到这个问题的简明答案,所以我在这里提出我的解决方案,希望它能帮助将来的某个人。
TL; DR:您需要使用BSTR在C#和C ++之间来回传递字符串。
我是这样做的。
以下是我的C#代码示例。有几点需要注意:
interface
部分中进行删除。 stringToPrint
参数的方式。 string
与[MarshalAs(UnmanagedType.BStr)]
的前缀至关重要。string
参数,就像它是普通字符串一样。您不需要像在C ++中那样使用C#中的 BSTR
转换。更多内容如下。//Reference where I got all this:
//https://support.microsoft.com/en-us/kb/828736
// Class1.cs
// A simple managed DLL that contains a method to add two numbers.
using System;
using System.Runtime.InteropServices;
namespace ManagedDLL
{
// Interface declaration.
public interface ICalculator
{
//Test functions
int Add(int Number1, int Number2);
int ReturnAge();
string StringTest();
void PrintAString([MarshalAs(UnmanagedType.BStr)] string stringToPrint);
};
// Interface implementation.
public class ManagedClass : ICalculator
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Test functions
public int Add(int Number1, int Number2)
{
return Number1 + Number2;
}
public int ReturnAge()
{
return 35;
}
public string StringTest()
{
return "Can you hear me now?";
}
public void PrintAString([MarshalAs(UnmanagedType.BStr)] string stringToPrint)
{
Console.WriteLine("Trying to print a BSTR in C#");
Console.WriteLine(stringToPrint);
Console.WriteLine("Done printing");
}
}
}
C ++中需要注意的一些事项:
#import
调用。这是你告诉C ++如何找到你的C#库的地方。我在问题链接到的教程中提到了这一点。AddTest()
函数。BSTR
类型变量传递给C#。将std::string
转换为BSTR
非常容易,我可以在任何一个方向进行转换。//csLink.h
#include <windows.h>
#include <iostream>
#include <string>
#import "path/to/C#/dll.tlb" raw_interfaces_only
using namespace std;
namespace Sample{
class CSLink {
public:
CSLink();
//Test functions
int AddTest(int i1, int i2);
int AgeTest();
string StringTestCall();
void stringArgTest(string s);
private:
ICalculatorPtr pCalc;
long lResult;
string convertBSTR(BSTR *s);
BSTR convertBSTR(string s);
};
}
//csLink.cpp
#include "stdafx.h"
#include "csLink.h"
using namespace std;
namespace Sample{
//Constructor
CSLink::CSLink(){
cout << "You have created a CS Link" << endl;
//https://support.microsoft.com/en-us/kb/828736
HRESULT hr = CoInitialize(NULL);
pCalc = ICalculatorPtr(__uuidof(ManagedClass));
lResult = 0;
}
//Test functions
int CSLink::AddTest(int i1, int i2){
cout << "you are adding " << i1 << " and " << i2 << endl;
pCalc->Add(i1, i2, &lResult);
cout << "The result should have been " << i1 + i2 << " and it was " << lResult << endl;
return 0;
}
int CSLink::AgeTest(){
cout << "Trying to get my age" << endl;
pCalc->ReturnAge(&lResult);
return lResult;
}
string CSLink::StringTestCall(){
BSTR s;
pCalc->StringTest(&s);
return convertBSTR(&s);
}
void CSLink::stringArgTest(string s)
{
//References I used figuring this all out:
//http://stackoverflow.com/questions/28061637/how-to-pass-string-parameters-between-c-and-c
//https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute(v=vs.110).aspx
//http://forums.codeguru.com/showthread.php?193852-How-to-convert-string-to-wstring
//http://stackoverflow.com/questions/6284524/bstr-to-stdstring-stdwstring-and-vice-versa
BSTR bSTR = convertBSTR(s);
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "Testing conversion: " << convertBSTR(&bSTR) << "|end test" << endl;
pCalc->PrintAString(bSTR);
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
//Utility functions
string CSLink::convertBSTR(BSTR *s){
if (*s == nullptr){
return "NULL STRING";
}
else{
wstring ws(*s, SysStringLen(*s));
string ss(ws.begin(), ws.end());
return ss;
}
}
BSTR CSLink::convertBSTR(string s){
wstring wStr = wstring(s.length(), L' ');
copy(s.begin(), s.end(), wStr.begin());
return SysAllocStringLen(wStr.data(), wStr.size());
}
}
关于它。对任何问题发表评论,我都会尽力回答。