[英]Calling C function with pointers from C#.NET code
我有以下問題:
我必須使用用C編寫的外部DLL庫,該庫實現了一個將兩個指針作為函數輸入的函數。 我不知道如何從C#中執行此操作...
typedef struct{
double[3] dArray;
int a;
int b;
}myInput
[...]
myFunction(myInput *input)
我可以在C#中定義類似的結構,但無法使我的代碼正常工作是否可以從C#中做到這一點? 先感謝您!
試試這樣的代碼。 我不確定int和double的大小。 c中的Int可以為16、32或64。double可以為4或8(通常為8)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("XXXXXXXX.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void myFunction(IntPtr input);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MyInput
{
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 3)]
double[] dArray;
int a;
int b;
}
static void Main(string[] args)
{
MyInput myInput = new MyInput();
IntPtr dataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myInput));
Marshal.StructureToPtr(myInput, dataPtr, true);
myFunction(dataPtr);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.