从C转换为C#,或生成DLL?

从C转换为C#,或生成DLL?,c#,c,C#,C,我不熟悉C#编程。我有一个用C修改的程序,我现在需要从我的C程序中获取字符串数组,并将其传递给C#程序,以便它可以使用这些值查询Oracle数据库 此程序用于从连接到计算机的所有iButton设备获取序列号 这是C代码 // function prototypes void UnLoadTMEX(void); short LoadTMEX(void); // globals static FARPROC Get_Version, TMGetTypeVersion, TMEndSession;

我不熟悉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);
}
//函数原型
无效卸载TMEX(无效);
短负荷TMEX(无效);
//全球的
静态FARPROC Get_Version、TMGetTypeVersion、TMEndSession;
静态FARPROC TMSetup、TMNext、TMRom、ExtendedStartSession;
静态FARPROC TMReadDefaultPort;
长(uu fastcall*TMExtendedStartSession)(短、短、空*);
静态铰链;
无符号字符状态_buf[5125];
//--------------------------------------------------------------------------
//iSeries 64的主要功能
//
void main(整型argc,字符**argv)
{
字符刷新,buf[200];
短标志,i,didsetup=0;
短ROM[9];
短PortNum,PortType;
长山丘;
char serialtmp[2],serial[10][17];
int j=-1;
//加载TMEX驱动程序并获取函数指针
如果(!LoadTMEX())
{
printf(“错误,无法加载IBFS64.DLL\n”);
出口(0);
}
//加载TMEX驱动程序并获取函数指针
TMReadDefaultPort(&PortNum,&PortType);
//获取TMEX驱动程序版本
printf(“端口号:%d端口类型:%d\n”,PortNum,PortType);
获取_版本(buf);
printf(“主驱动程序:%s\n”,buf);
printf(“类型%d:,端口类型”);
if((短)TMGetTypeVersion(PortType,buf)<0)
{
printf(“\n未找到此类型的硬件驱动程序!\n”);
//卸载TMEX驱动程序
UnLoadTMEX();
出口(0);
}
printf(“%s\n\n\n”,buf);
//检查命令行
如果(argc>1)
PortNum=atoi(argv[1]);
//检查PortNum的有效范围
如果((端口号<1)| |(端口号>15))
{
printf(“错误,请求的端口无效:%d\n”,PortNum);
出口(0);
}
//循环显示rom编号,直到按键
做
{
//获取请求端口的会话句柄
SHandle=TMExtendedStartSession(PortNum,PortType,NULL);
如果(SHandle>0)
{
//检查TMS设置是否已完成一次
如果(!didsetup)
{
flag=(短)TMS设置(SHandle);
如果(标志==1 | |标志==2)
{
printf(“TMSetup complete%d\n”,标志);
didsetup=1;
}
其他的
{
printf(“执行设置%d\n时出错”,标志);
打破
}
}
//仅在安装完成后获取下一个rom
其他的
{
//添加j是为了跟踪序列号字符串
j++;
memset(serial[j],0,strlen(serial[j]);
flag=(短)TMNext(SHandle,(void far*)和state_buf[0]);
如果(标志>0)
{
ROM[0]=0;
flag=(短)TMRom(SHandle,(void far*)和state_buf[0],(短-远*)和ROM[0]);
对于(i=7;i>=0;i--)
{
//这一部分与原来的部分不同
//将原始数字复制到字符串中
sprintf(serialtmp,“%02X”,ROM[i]);
strcat(串行[j],串行TMP);
}
printf(“%s”,序列号[j]);
printf(“\n”);
}
其他的
printf(“搜索结束”);
}
//关闭打开的会话
TMEndSession(SHandle);
}
} 
while(flag>0);
//卸载TMEX驱动程序
UnLoadTMEX();
printf(“iSeries 64结束\n”);
}
//--------------------------------------------------------------------------
//加载TMEX驱动程序并获取指向函数的指针
//
短负载TMEX(无效)
{
//尝试向TMEX驱动程序发送消息
hInst=LoadLibrary(L“IBFS64.DLL”);
//获取指向loopit64所需函数的指针
如果(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”);
//检查以确保获得所需的所有功能
if((ExtendedStartSession==NULL)| |(TMEndSession==NULL)||
(TMSetup==NULL)| |(TMNext==NULL)||
(TMRom==NULL)| |(Get_Version==NULL)||
(TMGetTypeVersion==NULL)| |(TMReadDefaultPort==NULL))
{
printf(“错误,无法获取指向所有对象的指针”
“所需的TMEX函数的数量\n”);
返回0;
}
//获取返回长字符串的函数指针
TMExtendedStartSession=(long(u fastcall*)(short,short,void*))ExtendedStartSession;
返回1;
}
其他的
返回0;
}
//--------------------------------------------------------------------------
//卸载TMEX驱动程序
//
void UnLoadTMEX(void)
{
//释放TMEX驱动程序
免费图书馆(hInst);
}
我应该尝试转换它,还是创建一个C DLL并将其导入我的C#程序?正如我所说,我并没有真正与C#一起工作过,我是一名实习生,我的老板给了我这个项目,让我可以学习C#。如果您想在c#read文章中使用c dll,我们将非常感谢您的帮助。
但是最好是将代码转换成c代码,因为你可以轻松地管理代码和更新代码。

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


我建议大家看看这本书,了解C#的详细介绍。

还有其他应用程序使用DLL吗?你需要男人吗