c# - 从 C 转换为 C#,还是制作 DLL?

标签 c# c

我是 C# 编程新手。我有一个用 C 语言修改的程序,现在需要获取从 C 程序获得的字符串数组并将其传递给 C# 程序,以便它可以使用这些值查询 Oracle 数据库。

此程序用于从连接到计算机的所有 iButton 设备获取序列号。

这是C代码

// function prototypes
void  UnLoadTMEX(void);
short LoadTMEX(void);

// globals
static FARPROC Get_Version, TMGetTypeVersion, TMEndSession;
static FARPROC TMSetup, TMNext, TMRom, ExtendedStartSession;
static FARPROC TMReadDefaultPort;
long (__fastcall *TMExtendedStartSession)(short,short,void *);

static HINSTANCE hInst;
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;


   // load the TMEX driver and get pointers to functions 
   if (!LoadTMEX())
   {
      printf("ERROR, could not load IBFS64.DLL\n");
      exit(0);
   }

   // load the TMEX driver and get pointers to functions 
   TMReadDefaultPort(&PortNum, &PortType);

   // get the TMEX driver version

   printf("Port number: %d     Port type: %d\n",PortNum,PortType);
   Get_Version(buf);
   printf("Main Driver: %s\n",buf);
   printf("TYPE%d:",PortType);
   if ((short)TMGetTypeVersion(PortType,buf) < 0)
   {
      printf("\nNo Hardware Driver for this type found!\n");
      // Unload the TMEX driver
      UnLoadTMEX();
      exit(0);
   }
   printf(" %s\n\n\n",buf);

   // check the command line
   if (argc > 1)
      PortNum = atoi(argv[1]);

   // check for valid range of PortNum
   if ((PortNum < 1) || (PortNum > 15))
   {
      printf("ERROR, invalid port requested: %d\n",PortNum);
      exit(0);
   }

   // loop to display the rom numbers until key hit
   do
   {
      // get a session handle to the requested port
      SHandle = TMExtendedStartSession(PortNum,PortType,NULL);
      if (SHandle > 0)
      {
         // check to see if TMSetup has been done once
         if (!didsetup)
         {
            flag = (short)TMSetup(SHandle);
            if (flag == 1 || flag == 2)
            {
               printf("TMSetup complete %d\n",flag);
               didsetup = 1;
            }
            else
            {
               printf("ERROR doing setup %d\n",flag);
               break;
            }
         }
         // only get the next rom after setup complete
         else
         {
             //j was added to keep track of the serial number strings
             j++;
             memset(serial[j], 0, strlen(serial[j]));
            flag = (short)TMNext(SHandle,(void far *)&state_buf[0]);
            if (flag > 0)
            {
                ROM[0] = 0;
                flag = (short)TMRom(SHandle, (void far *)&state_buf[0], (short far *)&ROM[0]);
                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);

                }
                printf("%s ", serial[j]);
                printf("\n");

            }
            else
               printf("end of search\n");
         }

         // close the opened session 
         TMEndSession(SHandle);
      }

   } 
   while (flag > 0);

   // Unload the TMEX driver
   UnLoadTMEX();

   printf("iSERIAL64 end\n");
}


//--------------------------------------------------------------------------
// Load the TMEX driver and get a pointers to the functions
//
short LoadTMEX(void)
{
   // attempt to get a SHandle to the TMEX driver
   hInst = LoadLibrary(L"IBFS64.DLL");

   // get a pointer to the function needed by loopit64
   if (hInst != NULL)
   {
      ExtendedStartSession = GetProcAddress(hInst,"TMExtendedStartSession");
      TMEndSession = GetProcAddress(hInst,"TMEndSession");
      TMSetup = GetProcAddress(hInst,"TMSetup");
      TMNext = GetProcAddress(hInst,"TMNext");
      TMRom = GetProcAddress(hInst,"TMRom");
      Get_Version = GetProcAddress(hInst,"Get_Version");
      TMGetTypeVersion = GetProcAddress(hInst,"TMGetTypeVersion");
       TMReadDefaultPort = GetProcAddress(hInst, "TMReadDefaultPort");

      // check to make sure got ALL of the functions needed
      if ((ExtendedStartSession == NULL) || (TMEndSession == NULL) ||
         (TMSetup == NULL) || (TMNext == NULL) ||
         (TMRom == NULL) || (Get_Version == NULL) ||
         (TMGetTypeVersion == NULL) || (TMReadDefaultPort == NULL))
      {
         printf("ERROR, could not get a pointer to all"
                " of the TMEX functions needed\n");
         return 0;
      }
      // get a function pointer that returns a long
      TMExtendedStartSession = (long (__fastcall *)(short,short,void *))ExtendedStartSession;

      return 1;
   }
   else
      return 0;
}


//--------------------------------------------------------------------------
// UnLoad the TMEX driver
//
void UnLoadTMEX(void)
{
   // release the TMEX driver
   FreeLibrary(hInst);
}

我应该尝试转换它,还是只是创建一个 C DLL 并将其导入到我的 C# 程序中?就像我说的,我还没有真正使用过 C#,我是一名实习生,我的老板给了我这个项目,以便我可以学习 C#。非常感谢任何帮助

最佳答案

如果你想在 c# 中使用 c dll,请阅读 this文章。 但最好将代码转换为 C# 代码,因为您可以轻松管理此代码并更新代码。

关于c# - 从 C 转换为 C#,还是制作 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709528/

相关文章:

c - 如何让 Xcode 3.2.3 正确包含 SpiderMonkey jsapi.h 文件?

c# - : When connecting to mysql database 附近的语法错误

c# - 如何在PropertyGrid中编辑根行中的可扩展对象?

c# - 为什么在使用 OleDb 导入 Excel 时忽略第一个空行

c# - 计算类文件中 appconfig 中的键数

c - 任何类型的变量的数字数组

c# - 值结构未在 WinRT 中初始化

c - 替代灵活数组成员,无需动态内存分配

c - 使用转义字符从 Sysfs 路径读取长值

c - 在父进程恢复执行之前等待所有子进程 UNIX