托管C++嵌入C#
来源:互联网 发布:建筑cad软件下载 编辑:程序博客网 时间:2024/05/18 01:26
http://blog.csdn.net/csethcrm/article/details/18261331
使用托管C++调用C#的exe程序。
(事后证明是对Reflector是无效的。)哎。
添加CLR控制台应用程序。把C#的exe程序放到“资源文件”文件夹下,右键属性的项类型设置为已编译的托管资源。
在主程序代码的main函数改写为如下内容:
1 // x.cpp: 主项目文件。 2 3 #include "stdafx.h" 4 5 using namespace System; 6 7 [STAThreadAttribute] 8 int main(array<System::String ^> ^args) 9 {10 System::Reflection::Assembly^ a;11 a = System::Reflection::Assembly::GetExecutingAssembly();12 System::IO::Stream ^ stream;13 stream = a->GetManifestResourceStream("csharp.exe");14 array<System::Byte> ^ bs = gcnew array<System::Byte>(stream->Length);15 stream->Read(bs, 0, (int)(stream->Length));16 System::Reflection::Assembly^ exe;17 exe = System::Reflection::Assembly::Load(bs);18 System::Reflection::MethodInfo^ info;19 info = exe->EntryPoint;20 array<System::Reflection::ParameterInfo^ > ^ parameters = info->GetParameters();21 if ((parameters != nullptr) && (parameters->Length > 0))22 {23 info->Invoke(nullptr, (array<System::Object ^ > ^) args);24 }25 else26 {27 info->Invoke(nullptr, nullptr);28 }29 return 0;30 }
其对应的C#版代码如下:
1 static class Program 2 { 3 /// <summary> 4 /// 应用程序的主入口点。 5 /// </summary> 6 [STAThread] 7 static void Main(string[] args) 8 { 9 var asm = Assembly.GetExecutingAssembly();10 Stream stream = asm.GetManifestResourceStream("csharp.exe");11 byte[] bs = new byte[stream.Length];12 stream.Read(bs, 0, (int)stream.Length);13 var exe = Assembly.Load(bs);14 MethodInfo info = exe.EntryPoint;15 ParameterInfo[] parameters = info.GetParameters();16 if ((parameters != null) && (parameters.Length > 0))17 info.Invoke(null, (object[])args);18 else19 info.Invoke(null, null);20 //Application.EnableVisualStyles();21 //Application.SetCompatibleTextRenderingDefault(false);22 //Application.Run(new Form1());23 }24 }
0 0
- 托管C++嵌入C#
- C#将托管DLL嵌入exe文件
- 【c++】C#调用C++ DLL 托管方式
- 利用反射动态调用C#嵌入到资源的托管DLL
- c#托管
- [C#/C++]C#调用非托管DLL的APIs
- [C#/C++]C#调用非托管DLL的APIs
- [C#/C++]C#调用非托管DLL的APIs
- c# 托管与非托管
- C#托管与非托管
- C# 托管与非托管
- c#托管与非托管
- C#托管与非托管
- C#调用非托管C编写DLL的数据类型转换
- C调用c#的托管字符串传送UTF8字符集方法
- C#调用非托管C++DLL中的函数
- C#导出非托管方法供C/C++调用
- C#调用非托管C++DLL的两种方法
- 全局替换安卓应用字体
- MySQL数据库基础(一)(启动-停止、登录-退出、语法规范及最基础操作)(持续更新中)
- eclipse svn 会报 文件夹 "" 已不存在
- #ASM 翻译系列第二十三弹:ASM Internal ASM files number 12 and 254
- UVA 1025 A Spy in the Metro城市里的间谍(dp)
- 托管C++嵌入C#
- #ASM 翻译系列第二十四弹:ASM Internal ASM files number 10 and 11
- 皮皮学Web第四弹——ServletContext
- #ASM 翻译系列第二十五弹:ASM 高级知识 When will my rebalance complete
- 转载Nexus-入门指南
- Java基础加强之集合篇(模块记忆、精要分析)
- pip install mysql-connector-python安装时报错
- 关于图片验证码返回二进制流,进行转换为Web的相对路径
- MySQL索引