如何在C中使用VB库?

时间:2014-02-28 10:45:04

标签: c++ c vb.net

我刚刚开始研究一个项目的C / C ++。我需要打开抽屉。

我使用的抽屉是Posiflex USB CR4500。

他们为程序员提供了一个抽屉的帮助指南。没有C / C ++的经验我至少可以说是有点困难。这个问题可能看起来有点长,但我确信这个解决方案非常简单,所以请继续阅读。

我正在使用WxDev进行编码,现在了解如何将脚本编译为exe。我遇到的问题是让实际代码工作甚至验证编译。这是供应商给出的说明(我被告知是非常基本的)。

  

Posiflex USB CashDrawer系列USB CR DLL v1.03编程指南

     

驾驶员需要控制Posiflex USB现金抽屉   被称为“USBCR.DLL”。当Posiflex USB现金抽屉测试   程序已安装,此驱动程序存储在文件夹中   “C:\ Windows \ System”如果是默认的Windows文件夹   “C:\ WINDOWS”。请保留此文件“USBCR.DLL”   用于控制Posiflex USB的软件的系统文件夹   即使您想要卸载Posiflex测试,也可使用现金抽屉   程序

     

驱动程序“USBCR.DLL”提供7个函数调用:   1--5功能适用于USB CashDrawer系列。   6--7功能适用于USB CashDrawer系列。   Visual Basic的示例如下所示。

     

1.Public Declare Function OpenUSBcr LIB“usbcr.dll”()As Long

     在调用其他函数之前必须调用

     

成功时返回0

     

2.Public Declare Function CloseUSBcr Lib“usbcr.dll”()As Long

     

在退出程序之前调用此函数

     

成功时返回0

     

3.Public Declare Function DrawerOpen Lib“usbcr.dll”(ByVal ID As Long)As Long

     

成功发送命令时返回0,错误时返回-1

     

'ID'是抽屉编号,从0到7

     

4.Public Declare Function DrawerState Lib“usbcr.dll”(ByVal ID As Long)As Long

     

返回抽屉状态

     

高半字节是抽屉ID,

     如果抽屉打开,

低半字节为0,如果抽屉关闭则为1

     

错误时返回-1

     

5.Public Declare Function RetrieveStatistics Lib“usbcr.dll”(ByVal ID As Long,ByVal idx As Long,ByRef buf As Any,ByVal size As Long)As Long

     

成功发送命令时返回0,错误时返回-1

     

'size'是'buf'

可以保存的字节数      

'buf'将保存从现金抽屉中的内存中读取的值

     

此功能用于检索Unified POS1.8的设备统计信息。

     

6.Public声明函数OpenUSB Lib“usbcr.dll”()As Long

     在调用其他函数之前必须调用

     

成功时返回0

     

此功能适用于USB CashDrawer系列,

     

它也可以开车。

     

7.Public Declare Function CloseUSB Lib“usbcr.dll”()As Long

     

在退出程序之前调用此函数

     

成功时返回0

     

此功能适用于USB CashDrawer系列。

     

它也可以驱动USB CashDrawer。

     

注意:对于C ++程序:使用__stdcall编译器选项

所以我需要知道如何编写这些我在C中假设的,因为底部的消息。

到目前为止我尝试了什么?

此链接http://www.autoitscript.com/forum/topic/94473-calling-usbcrdll-to-open-a-cash-drawer/向我提供了此代码:

#include <stdio.h>

int main(int argc, char *argv[])
{

$usbcr=DllOpen("usbcr.dll")
$call=DllCall($usbcr,"long","OpenUSBcr")
If @error Or $call[0]<>0 Then
  ConsoleWrite("Fail."&@CRLF)
Else
  ConsoleWrite("Success."&@CRLF)
Endif
}

然而,这在第一道障碍时失败了。我也有这个链接 http://msgboard.alphasoftware.com/alphaforum/showthread.php?95962-calling-a-dll-in-order-to-open-a-cash-drawer

#include <stdio.h>

int main(int argc, char *argv[])
{
declare usbcr OpenUSBcr L
declare usbcr DrawerOpen LL

aa = OpenUSBcr()
bb = DrawerOpen(7)   '7 is the drawer identifier
}

这也失败了,虽然我确信这是我完全没有使用C的经验。

不是粘贴我正在看的其他二十个左右的链接,下面是一些实际上似乎在一定程度上起作用的代码,尽管它打印状态而不是打开直到。我在两台机器上运行了这个代码,一台带有直通,一台没有。当我在机器上打印OpenUSBcr而没有得到-2时,在带有直到我得到0的那个上,因此它正确地获得状态。

#include <stdio.h>
#include <time.h>

#include "windows.h"

int __cdecl main(int argc, char* argv[])
{
  HMODULE hm;
  long (*ou)();
  long (*cu)();
  long (*wp)(long);
  long (*ps)(long);

  SetLastError( 0);
  hm = LoadLibrary( "usbcr.dll");
  printf( " hm = %p, %lu \n",hm,GetLastError());
  if ( hm==NULL ) return 1;

  SetLastError( 0);
  ou = (long(*)()) GetProcAddress( hm,"OpenUSBcr");
  printf( " ou = %p, %lu \n",ou,GetLastError());

  SetLastError( 0);
  cu = (long(*)()) GetProcAddress( hm,"CloseUSBcr");
  printf( " cu = %p, %lu \n",cu,GetLastError());

  SetLastError( 0);
  wp = (long(*)(long)) GetProcAddress( hm,"DrawerOpen");
  printf( " wp = %p, %lu \n",wp,GetLastError());

  SetLastError( 0);
  ps = (long(*)(long)) GetProcAddress( hm,"DrawerState");
  printf( " ps = %p, %lu \n",ps,GetLastError());

  printf( " OpenUSBcr = %ld \n", ou());
  for (long i=0;i<8;++i) // test 8 drawers
  {
    printf( " st1(%ld) = %ld \n", i,ps(i));
    printf( " ocr(%ld) = %ld \n", i,wp(i));
    printf( " st2(%ld) = %ld \n", i,ps(i));
  }
  printf( " CloseUSBcr = %ld \n", cu());

  FreeLibrary( hm);
  return 0;
}

取自https://forums.codegear.com/message.jspa?messageID=435296。解决方案可能就在我面前,但任何人都可以给予我的任何帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:3)

我没有时间,因此不需要提供现成的代码,但我可以尝试提供帮助。

您尝试使用的库是用VB编写的,这就是您的前两个代码在VB中的原因。最后一个代码加载库(LoadLibrary,似乎是windows.h的一个函数)来执行API。

我根本不知道前两个代码的用途,所以我会尝试分析最后一个代码。

///* ... */在C)中发表评论。

根据我所知道的C,我做了很多疯狂的猜测,如果有人能证实或证实它们,我会非常感激它。

#include <stdio.h> //Standard library, include printf, 
#include <time.h> //Time library, don t know why it is included

#include "windows.h" //Wild guess: include LoadLibrary, SetLastError, GetLastError...

int __cdecl main(int argc, char* argv[]) //Basic main declaration, with the addition of __cdecl, which clean the stack (don t know why it is useful)
{
  HMODULE hm; //Structure, probably included in windows.h
  long (*ou)(); //Declare a pointer to function (Wild guess)
  long (*cu)();
  long (*wp)(long);
  long (*ps)(long);

  SetLastError( 0); //Clean the error
  hm = LoadLibrary( "usbcr.dll"); //Load usbcr to use its function
  printf( " hm = %p, %lu \n",hm,GetLastError()); //Print error, if it is 0, all did gone well
  if ( hm==NULL ) return 1; //If the library failed to load, terminate the execution

  SetLastError( 0);
  ou = (long(*)()) GetProcAddress( hm,"OpenUSBcr"); //Wild guess: assign OpenUSBcr to the pointer
  printf( " ou = %p, %lu \n",ou,GetLastError());

  SetLastError( 0);
  cu = (long(*)()) GetProcAddress( hm,"CloseUSBcr");
  printf( " cu = %p, %lu \n",cu,GetLastError());

  SetLastError( 0);
  wp = (long(*)(long)) GetProcAddress( hm,"DrawerOpen");
  printf( " wp = %p, %lu \n",wp,GetLastError());

  SetLastError( 0);
  ps = (long(*)(long)) GetProcAddress( hm,"DrawerState");
  printf( " ps = %p, %lu \n",ps,GetLastError());

/*Previous assignement give us this:
  -ou is OpenUSBcr
  -cu is CloseUSBcr
  -wp is DrawerOpen
  -ps is DrawerState
*/

  printf( " OpenUSBcr = %ld \n", ou()); //Open the device
  for (long i=0;i<8;++i) //For each drawer (assuming there is 8)
  {
    printf( " st1(%ld) = %ld \n", i,ps(i)); //Get and print the state
    printf( " ocr(%ld) = %ld \n", i,wp(i)); //Print if the drawer is open
    printf( " st2(%ld) = %ld \n", i,ps(i));
  }
  printf( " CloseUSBcr = %ld \n", cu());//Close the device

  FreeLibrary( hm); //Unload the library (memory management)
  return 0; //Terminate normally
}

基本上,这个C程序以可用的C形式“导入”库,并使用thoses来检查八个抽屉的状态。

您可以编辑for部分来制作您的程序,假设您学习了C并且您的任务很简单。