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

我是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)  1) PortNum = atoi(argv[1]); // check for valid range of PortNum if ((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,请阅读本文。 但将代码转换为c#代码更好,因为您可以轻松管理此代码并更新代码。

如果你的老板给你这个项目学习C#,我会说继续把它转换成C#。 它将使代码的未来使用变得更加容易。

我建议在深度查看C#中的C#介绍C#。