gpt4 book ai didi

c++ - 如何从非托管 C++ 调用托管 C++ 方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:52 25 4
gpt4 key购买 nike

请查看下面的更新

(已解决)此外,我已将其扩展为第二个问题 Implement a C# DLL COM File In Unmanaged C++ Program

我已经研究了 Internet 的尽头,但没有找到一个真实的、可以理解的、人类的例子来说明如何做到这一点。

我有一个加密和解密文本的 C# DLL。

我不想/没有智力在 C++ 非托管代码中重写它。因此,我创建了一个与 C# dll 接口(interface)的 C++/CLR 类。

现在我需要知道如何从非托管代码调用托管 C++。

这是我的托管代码,已验证它可以工作

// clrTest.cpp : main project file.

#include "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>

using namespace cSharpRiJHarn;
using namespace System;


String^ Encrypt(String ^s)
{
return RijndaelLink::encrypt(s);
}


String^ Decrypt(String ^s)
{
return RijndaelLink::decrpyt(s);
}

int main()
{
//Console::WriteLine(Encrypt("It Works"));

//Console::WriteLine(Decrypt(Encrypt("It Works")));

//Console::ReadLine();
return 0;
}

现在我再次研究了这个。

我已经看到了所有糟糕/过于复杂的解释

我知道我需要使用称为 COM 或 Interop 的东西

我不知道它是如何工作的,我只是在寻找一个非常简单的解释。

感谢您的帮助。

更新

我已将 C# DLL 转换为 COM 文件

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cSharpRiJHarn
{
[Guid("GuiD CODE REMOVED")]
public interface DBCOM_Interface
{
[DispId(1)]
String encrypt(string s);
[DispId(2)]
String decrpyt(string s);
}

[Guid("GuiD CODE REMOVED"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface DBCOM_Events
{
}

[Guid("GuiD CODE REMOVED"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(DBCOM_Events))]
public class RijndaelLink : DBCOM_Interface
{
public String encrypt(String s)
{
return Rijndael.EncryptString(s);
}
public String decrpyt(String s)
{
return Rijndael.DecryptString(s);
}
}
}

现在我只需要知道如何在非托管 C++ 中实现它...我已尝试将文件仅添加到 C++ 项目,并将整个 cSharpRiJHarn 项目添加到此解决方案。两者都不起作用。

#import "cSharpRiJHarn" 
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <iostream>
//using namespace cSharpRiJHarn;


int main(){

cSharpRiJHarn::RijndaelLink::encrypt("It works");
char ch;
std::cin>>ch;
return 0;
}

这是我遇到的错误之一。

Error 6 error C2653: 'cSharpRiJHarn' : is not a class or namespacename

Error 8 IntelliSense: cannot open source file"C:/.../.../Documents/Visual Studio2010/Projects/unmannagedCPPExample/unmannagedCPPExample/Debug/cSharpRiJHarn.tlh" c:......\documents\visualstudio2010\projects\unmannagedcppexample\unmannagedcppexample\unmannagedcppexample.cpp

最佳答案

你可以使用很酷的 C++ Marshaling library由 Microsoft 提供,如下所示:

#include "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include "msclr\marshal_cppstd.h" // marshaling library

using namespace cSharpRiJHarn;
using namespace System;
using namespace msclr::interop; // marshaling library

std::wstring Encrypt(std::wstring s)
{
return marshal_as<std::wstring>(RijndaelLink::encrypt(marshal_as<String^>(s)));
}

std::wstring Decrypt(std::wstring s)
{
return marshal_as<std::wstring>(RijndaelLink::decrypt(marshal_as<String^>(s)));
}

关于c++ - 如何从非托管 C++ 调用托管 C++ 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com