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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - 漫步人生

c# 使用GetOleDbSchemaTable获取access数据库结构 xp上vs2010+IE8无法调试脚本 iis 上部署asp.net程序出错排查 windows2008的共享 使用sqlite绿色部署问题 在vs中使用正则表达式 - 漫步人生 事件不应该有返回值 ae的com对象在dotnet中的释放问题 adf for dotnet中servercontext的释放 修改Windows中ASPNET账户的密码 ae显示arcserver的地图服务 arcgis 编辑时独占mdb - 漫步人生 adf for dotnet中动态给ArcGis Server服务添加图层 js中在不知道属性名时获取属性对象 html文档中的name属性 ie中js a光标位置 adf中UpdatePanel刷新地图 windows局域网内以arcgis server local的方式连接arcserver服务器 c#与 如何调试
C#与C/C++的互操作
漫步人生 · 2011-01-14 · via 博客园 - 漫步人生

C#可以调用C的导出函数,使用DllImportAttribute定义一个静态方法来调用C的导出函数,需要注意的是参数的传递,不同的参数类型传递方法有很大的差异。详细可以参见http://blog.csdn.net/Mittermeyer/archive/2007/04/27/1586867.aspx

主要是使用Marshal类进行内存跟C#之间的转换的操作

实际上C#的参数传递到C时,值类型就是直接传递值,指针的传递有时需要使用ref,这里不清楚运行时是否有再申请一块非托管内存来保存托管代码传过来的值。

基本类型数组的传递会变为数组首地址指针的传递,应该是运行时申请了一块非托管内存来保存数组的值,再将这块内存的首地址传给C函数。

如前面链接的网页描述的结构数组的传递,就是这样,如果要把数组的值传过去,则需要在申请内存后使用Marshal的StructureToPtr方法把托管内存里的数组值写到非托管内存中,在非托管内存中修改这些值之后,使用Marshal的PtrToStructure方法把非托管内存里的值复制到托管内存中。

自定义类,按照上面处理结构数组的方法思路也可以处理自定义类,只是类似StructureToPtr和PtrToStructure的值的复制操作需要自己实现。

委托的传递是使用hunk技术来实现的。这样可以实现C回调C#方法。