使用PInvoke在c#中等效C unsigned char数组

时间:2014-02-13 20:46:30

标签: c# c pinvoke

我昨天发布了关于如何将C程序转换为C#作为我的第一个C#项目:Convert from C to C#, or make DLL?。我已经取得了很大的进步,但我被困在了一个部分。我不知道如何重新创建这个unsigned char数组

C代码:

unsigned char state_buf[5125];

//--------------------------------------------------------------------------
// Main of iSerial64
//
void main(int argc, char **argv)
{
   char refresh,buf[200];
   short flag,i,didsetup=0;
   short ROM[9];
   short PortNum,PortType;
   long SHandle;
   char serialtmp[2], serial[10][17];
   int j = -1;

...

这是C#中使用PInvoke http://files.maximintegrated.com/sia_bu/licensed/docs/1-wire_sdk_win/TMEX/romm1rxq.html

获取state_buf的函数

我用来从api导入函数的C#代码:

 [DllImport("IBFS64.dll")]
        public static extern short TMRom(
            int session_handle,
            byte state_buf,
            [MarshalAs(UnmanagedType.LPStr)]StringBuilder ROM
        );

我看到那个字节是C#的等价物,但我不完全确定如何使用它。

到目前为止,你们都非常乐于助人,我对我在这方面所取得的进展感到非常满意。

这是我从昨天开始在C#中完成的事情:

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

namespace PInvokeTest {
  class Program {
    static void Main(string[] args)
      {
          int session_handle = 0;
          int result = 0;
          int flag = 0;
          int didsetup = 0;
          int defPort = 0;
          int i = 0, j = -1;
          short type_test = 0;
          byte state_buf = 0;
          short port_num = 0, port_type = 1;
          StringBuilder ID_buf = new StringBuilder();
          StringBuilder serial = new StringBuilder();
          StringBuilder serialtmp = new StringBuilder();
          StringBuilder ROM = new StringBuilder();

          //result = TMSearch(session_handle, state_buffer, 1, 1, 0xEC);


          defPort = TMReadDefaultPort(out port_num,out port_type);


          if (defPort == 1)
          {
              Console.Write("Default Device: ");

              switch (port_type)
              {
                  case 1:
                      Console.Write("DS9097E serial adapter ");
                      break;

                  case 2:
                      Console.Write("Parallel adapter ");
                      break;

                  case 5:
                      Console.Write("DS90907U serial adapter ");
                      break;

                  case 6:
                      Console.Write("DS9490 USB adapter ");
                      break;
              }
              Console.Write("on port # {0}", port_num);
          }

          else
              Console.Write("No Device found");

          // get the TMEX driver version
          Get_Version(ID_buf);

          Console.WriteLine("\nDriver: {0}", ID_buf);

          type_test = TMGetTypeVersion(port_type, ID_buf);

          if (type_test < 0)
          {
              Console.WriteLine("\nNo Hardware Driver for this type found!\n");
              Console.Write("Press any key to exit");
              Console.ReadKey();
              Environment.Exit(0);
          }
          else
              Console.WriteLine("Driver Found");


          // check for valid range of PortNum
          if ((port_num < 1) || (port_num > 15))
          {
              Console.WriteLine("ERROR, invalid port requested: {0}\n", port_num);
              Console.Write("Press any key to exit");
              Console.ReadKey();
              Environment.Exit(0);
          }


          do
          {
              // get a session handle to the requested port
              session_handle = TMExtendedStartSession(port_num, port_type, IntPtr.Zero);

              if (session_handle > 0)
              {
                  // check to see if TMSetup has been done once
                  if (didsetup == 0)
                  {
                      flag = TMSetup(session_handle);
                      if (flag == 1 || flag == 2)
                      {
                          Console.Write("TMSetup complete {0}", flag);
                          didsetup = 1;
                      }
                      else
                      {
                          Console.Write("ERROR doing setup {0}", flag);
                          break;
                      }
                  }

                  else
                  {
                      //j was added to keep track of the serial number strings
                      j++;

                      //memset(serial[j], 0, strlen(serial[j]));
                      flag = TMNext(session_handle, state_buf);
                      if (flag > 0)
                      {
                          //ROM[0] = 0;
                          flag = TMRom(session_handle, state_buf, ROM);
                          for (i = 7; i >= 0; i--)
                          {
                              //This section was changed from the original
                              //copies raw number into string
                              //sprintf(serialtmp, "%02X", ROM[i]);
                              //strcat(serial[j], serialtmp);

                          }
                          Console.WriteLine("{0} \n", serial[j]);

                      }
                      else
                          Console.WriteLine("end of search\n");
                  }
                  // close the opened session
                  TMEndSession(session_handle);
              }

          }
          while (flag > 0);






          Console.ReadKey();
      }

   [DllImport("IBFS64.dll")]
    public static extern int TMExtendedStartSession(
        short PortNum, 
        short PortType,
        IntPtr EnhancedOptions 
    );

    [DllImport("IBFS64.dll")]
    public static extern short TMSearch(
        int session_handle, 
        byte state_buffer, 
        short p1, 
        short p2, 
        short p3
    );

    [DllImport("IBFS64.dll")]
    public static extern short TMReadDefaultPort(
        out short port_num,
        out short port_type
    );

    [DllImport("IBFS64.dll")]
    public static extern short Get_Version(
        [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf 
    );

    [DllImport("IBFS64.dll")]
    public static extern short TMGetTypeVersion(
        short port_type,
        [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
    );

    [DllImport("IBFS64.dll")]
    public static extern short TMSetup(
        int session_handle
    );

    [DllImport("IBFS64.dll")]
    public static extern short TMNext(
        int session_handle,
        byte state_buf
    );

    [DllImport("IBFS64.dll")]
    public static extern short TMRom(
        int session_handle,
        byte state_buf,
        [MarshalAs(UnmanagedType.LPStr)]StringBuilder ROM
    );

    [DllImport("IBFS64.dll")]
    public static extern short TMEndSession(
        int session_handle
    );
  }
}

如果有什么我应该改变,请告诉我。我很感激帮助!

1 个答案:

答案 0 :(得分:9)

对于C语言数组:

unsigned char state_buf[5125] ;

尝试C#

byte[] state_buf = new byte[5125] ;

byte是一个无符号的8位数字,域名为0-255。如果您希望对八位字节进行签名,请使用sbyte(域-128到+127)。