2017-08-14 50 views
0

我有用C編寫的庫(*.dll文件)。我試圖與我正在構建的Windows Form應用程序一起使用它。我重新定義了導入工作所需的結構。我怎樣才能給結構中的變量賦值。如何使用C#文本框輸入與C結構和導入

[DllImport("hdnpic.dll")] 
     public static extern int Hidden(Hid4cinp hid4cinp); 

     [StructLayout(LayoutKind.Sequential)] 
     public struct Hid4cinp 
     { 
      public const int maxSize = 500; 
      public double alpha_c; 
      public double alpha_m; 
      public double alpha_y; 
      public double alpha_k; 
      public double freq; 
      public double dsmp; 
      public double cdxy; 
      public double cdhi_c; 
      public double cdhi_m; 
      public double cdhi_y; 
      public double cdhi_k; 
      public double amp1; 
      public double perd; 
      public int funtype; 
      public int smooth; 
      public int inpres; 
      public int width; 
      public int height; 


     } 

我已經用C#編寫了代碼。有人可以建議我如何將值分配給Hid4cinp結構變量。

+0

C是不一樣的C++。除非C++相關,否則應該刪除C++標記。 –

+0

我看不到「* native C code *」,但只有C#代碼。 – alk

回答

0

嘗試以下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

namespace ConsoleApplication73 
{ 
    class Program 
    { 
     [DllImport("hdnpic.dll")] 
     public static extern int Hidden(IntPtr hid4cinp); 

     [StructLayout(LayoutKind.Sequential)] 
     public struct Hid4cinp 
     { 
      public const int maxSize = 500; 
      public double alpha_c; 
      public double alpha_m; 
      public double alpha_y; 
      public double alpha_k; 
      public double freq; 
      public double dsmp; 
      public double cdxy; 
      public double cdhi_c; 
      public double cdhi_m; 
      public double cdhi_y; 
      public double cdhi_k; 
      public double amp1; 
      public double perd; 
      public int funtype; 
      public int smooth; 
      public int inpres; 
      public int width; 
      public int height; 


     }   
     static void Main(string[] args) 
     { 

      Hid4cinp hid4cinp = new Hid4cinp(); 

      hid4cinp.alpha_c = 123; 

      IntPtr hid3cinpPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hid4cinp)); 
      Marshal.StructureToPtr(hid4cinp, hid3cinpPtr, true); 

      int results = Hidden(hid3cinpPtr); 
     } 
    } 

} 
+0

感謝@jdweng的想法,但它的'hid4cinp.alpha_c = 123;'那就是鬥爭。如何使用來自win表單文本框(C#)的輸入並分配給該結構的變量? –

+1

hid4cinp.alpha_c = double.Parse(textbox1.txt); – jdweng