DllImport in C#

strongly refer: http://www.pinvoke.net/default.aspx/kernel32.GetSystemInfo

 

DllImport("kernel32.dll", EntryPoint = "CreateFile")]static extern IntPtr CreateFile(string filename,MarshalAs(UnmanagedType.U4)]FileAccess fileaccess,MarshalAs(UnmanagedType.U4)]FileShare fileshare,int securityattributes,MarshalAs(UnmanagedType.U4)]FileMode creationdisposition,int flags,IntPtr template);

//[DllImport("kernel32.dll", EntryPoint = "CreateFile")]

//private static extern IntPtr CreateFile(string lpFileName, System.Int32 dwDesiredAccess, // System.Int32 dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, // System.Int32 dwCreationDisposition, System.Int32 dwFlagsAndAttributes, // IntPtr hTemplateFile); DllImport("kernel32.dll", EntryPoint = "SetFilePointer")]private static extern uint SetFilePointer(IntPtr hFile, int lDistanceToMove,IntPtr lpDistanceToMoveHigh, uint dwMoveMethod);

 

 

 

 

 

//[DllImport("kernel32.dll", EntryPoint = "SetFilePointer")] //private static extern System.Int32 SetFilePointer( // IntPtr hFile,

 

 

 

 

 

//[DllImport("kernel32.dll", EntryPoint = "WriteFile")] //private static extern bool WriteFile(

 

 

 

 

 

 

 

//public void SetStamp(string strFile, System.Int32 i32Stamp) //{

 

 

 

 

// byte[] buffer = { (byte)(i32Stamp << 16 >> 24), // (byte)(i32Stamp << 24 >> 24), // (byte)(i32Stamp >> 24), // (byte)(i32Stamp << 8 >> 24) }; // uint uiWrittenLen;

 

 

 

 

 

 

 

// System.Threading.NativeOverlapped lpOverlapped = new NativeOverlapped(); // IntPtr filePtr = CreateFile(strFile, FileAccess.ReadWrite, FileShare.ReadWrite, // 0, FileMode.Open, 0, IntPtr.Zero); // SetFilePointer(filePtr, 8, IntPtr.Zero, 0); //why does it not work? // WriteFile(filePtr, buffer, (uint)(buffer.Length), out uiWrittenLen, ref lpOverlapped); // CloseHandle(filePtr); //}

// IntPtr hFile, // LPCVOID lpBuffer, // System.Int32 nNumberOfBytesToWrite, // LPDWORD lpNumberOfBytesWritten, // LPOVERLAPPED lpOverlapped); DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true)]return: MarshalAs(UnmanagedType.Bool)]public static extern bool CloseHandle(IntPtr hObject);

// LONG lDistanceToMove, // PLONG lpDistanceToMoveHigh, // System.Int32 dwMoveMethod); DllImport ( "kernel32.dll" , EntryPoint = "WriteFile" )] private static extern bool WriteFile( IntPtr hFile, byte [] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, In ] ref System.Threading. NativeOverlapped lpOverlapped);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是一个在C#中使用DllImport调用C/C++动态链接库的语法。在C#中,DllImport是一个特殊的属性,它可以将C/C++动态链接库中的函数导入到C#程序中,从而可以在C#中调用这些函数。以下是一个简单的例子,演示如何在C#中使用DllImport调用一个名为“mylib”的动态库中的函数: ```csharp using System; using System.Runtime.InteropServices; class MyLibrary { [DllImport("mylib.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int yh_baselinecall(IntPtr InData, IntPtr OutData); static void Main() { string input = "Hello, world!"; string output = new string(' ', input.Length); IntPtr inData = Marshal.StringToHGlobalAnsi(input); IntPtr outData = Marshal.StringToHGlobalAnsi(output); int result = yh_baselinecall(inData, outData); Marshal.Copy(outData, output.ToCharArray(), 0, output.Length); Console.WriteLine("Result: " + result); Console.WriteLine("Output: " + output); Marshal.FreeHGlobal(inData); Marshal.FreeHGlobal(outData); } } ``` 在这个例子中,我们首先使用DllImport属性将名为“yh_baselinecall”的函数从动态库“mylib.dll”中导入到C#程序中。 在Main方法中,我们创建了一个输入字符串input和一个长度相同的空字符串output。然后,我们使用Marshal.StringToHGlobalAnsi方法将输入字符串和空字符串分别转换为IntPtr类型的输入数据inData和输出数据outData。 接下来,我们调用yh_baselinecall函数并传递inData和outData作为参数。在函数调用结束后,我们使用Marshal.Copy方法将输出数据outData中的内容复制到输出字符串output中。 最后,我们打印函数返回值result和输出字符串output,并使用Marshal.FreeHGlobal方法释放inData和outData所占用的内存空间。 需要注意的是,你需要将mylib.dll文件放在C#程序的运行目录下,或者使用绝对路径指定动态库文件的位置。 希望这个简单的例子能够帮助你解决问题。如果有其他问题,可以继续提出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值