惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
D
Docker
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
IT之家
IT之家
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
B
Blog
D
DataBreaches.Net

博客园 - sunrack

LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains ASPxPopupControl does not show up in the right place 程序员从初级到中级10个秘诀 Get JavaScript IntelliSense With DevExpress Client-Side Objects - v2010 vol 1 Grid Editing - Cascading Combo Boxes - sunrack ASPxRadioButtonList 报错解决办法 - sunrack - 博客园 本地连接属性“出现意外错误”的解决办法 - sunrack - 博客园 Bootstrap DotNetFX35SP1 in Visual Studio 2010 Windows Server 2008 共享策略 XtraPivotGrid Custom Summaries 手动注册 DevExpress 8.2.3 控件到 Visual Studio 工具箱 sn.exe error "Failed to generate a strong name key pair -- The keyset is not defined" Thread Tools Rate Thread 浅析JavaScript中的showModalDialog的实战应用 javascript showModalDialog模态对话框使用说明 - sunrack - 博客园 entity framework 缓存干扰的数据不一致问题 Failed to load viewstate ? Typical problem, with an obvious solution. ViewState and Dynamic Control ASPxGridView 和 AJAX Extensions ToolBox 不兼容 ASPxCombobox Features
C++/CLI托管字符串与非托管char数组的转换
sunrack · 2011-01-11 · via 博客园 - sunrack

CLI

显示行号 复制代码 这是一段程序代码。

  1. // CLR.Dll.h
    
  2. 
    
  3. #pragma once
    
  4. #include "string.h"
    
  5. 
    
  6. 
    
  7. using namespace System;
    
  8. using namespace System::Runtime::InteropServices;
    
  9. namespace CLRDll {
    
  10.     public ref class DllClass
    
  11.     {
    
  12.     public:
    
  13.         String^ FunctionTest(String^ input)
    
  14.         {
    
  15.             char* inputChar = ManagedString2UnmanagedStringA(input);
    
  16.             //call native c++ function...
    
  17.             inputChar[0] = 'A';
    
  18.             String^ result = UnmanagedStringA2ManagedString(inputChar);
    
  19.             return result;
    
  20.         }
    
  21.     private:
    
  22.         //将?非?托D管ü的?ANSI字?符?串?转a换?成é托D管ü字?符?串? 
    
  23.         String^ UnmanagedStringA2ManagedString(char* pIn)
    
  24.         {
    
  25.             return Marshal::PtrToStringAnsi(static_cast<IntPtr>(pIn));
    
  26.         }
    
  27.         //将?托D管ü字?符?串?转a换?成é非?托D管ü的?ANSI字?符?串? 
    
  28.         char* ManagedString2UnmanagedStringA(String^ strIn)
    
  29.         {
    
  30.             IntPtr ip = Marshal::StringToHGlobalAnsi(strIn);
    
  31.             const char* pTemp = static_cast<const char*>(ip.ToPointer());
    
  32.             char *pOut = new char[strlen(pTemp)+1];
    
  33.             strcpy(pOut, pTemp);
    
  34.             Marshal::FreeHGlobal(ip);
    
  35.             return pOut;
    
  36.         } 
    
  37.     };
    
  38. }
    

C#

显示行号 复制代码 这是一段程序代码。

  1. public void RunTest()
    
  2. {
    
  3.     DllClass curDllClass = new DllClass();
    
  4.     string input = "test";
    
  5.     string output = curDllClass.FunctionTest(input);
    
  6. }