• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张继东
看铁蹄铮铮,踏遍万里河山。磨不破,打不烂。
   首页    新随笔    联系   管理    订阅  订阅

c/c++和.net之间相互调用

c/c++和.net之间相互调用,代码转换工具:

1.http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe

2.c++/c#互转工具

 

具体转换代码:

代码
 static object BytesToStruct(byte[] bytes, Type strcutType)
        {

            
int size = Marshal.SizeOf(strcutType);
            IntPtr buffer 
= Marshal.AllocHGlobal(size);
            
try
            {
                Marshal.Copy(bytes, 
0, buffer, size);
                
return Marshal.PtrToStructure(buffer, strcutType);
            }
            
catch (Exception ex)
            {
                Console.Write(
"BytesToStruct,error{0}\r\n", ex.ToString());
                
return null;
            }
            
finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
        
static byte[] StructToBytes(object structObj)
        {
            
int size = Marshal.SizeOf(structObj);
            IntPtr buffer 
= Marshal.AllocHGlobal(size);
            
try
            {
                Marshal.StructureToPtr(structObj, buffer, 
false);
                
byte[] bytes = new byte[size];
                Marshal.Copy(buffer, bytes, 
0, size);
                
return bytes;
            }
            
finally
            {
                Marshal.FreeHGlobal(buffer);
            }

        }   

 

 

 

posted @ 2010-06-08 09:40  简单生活  阅读(1552)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3