How to use C-Library in C#

亡梦爱人 提交于 2019-12-02 20:04:34

问题


i´ve downloaded a BACnet-Stack from http://sourceforge.net/projects/bacnet/ but it is written in c and i want to use it in c#.

I´ve been reading for 4 hours now about how to get it done but i´m not any further. Most answers are to write the code anew in c# but i have no clue of c . I opened a workspace in Code::Blocks to look into the code and compiled a library into a a.-file. But how can i use it?

Greetings,

Stefan


回答1:


To address alike situation, Microsoft provides attributes, assembly, and marshaling to offer interoperability between managed-unmanaged code(not .net aware/running outside the clr boundaries) and managed-legacy COM.
Investigate the use of dynamics and the (Dynamic language runtime- DLR) which should be more than fine.
code example (using kernel32.dll) as an example of calling unmanaged code from a managed context

[DllImport("kernel32.dll", EntryPoint="MoveFile",
ExactSpelling=false, CharSet=CharSet.Unicode,
SetLastError=true)]
static extern bool MoveFile(string sourceFile, string destinationFile);

//calling the function
static void Main()
{
    MoveFile("sheet.xls", @"c:\sheet.xls");
}

check this pdf also: http://www.nag.com/IndustryArticles/Calling_C_Library_DLLs_from_C_Sharp.pdf



来源:https://stackoverflow.com/questions/17745329/how-to-use-c-library-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!