当前位置:编程学习 > C#/ASP.NET >>

vb.net中使用C++中带LPCTSTR参数的DLL的问题

有一个用C++编写的DLL,有一个函数的调用参数是LPCTSTR。我使用VB.NET调用这个DLL,如果把这个参数定义为String,那么传递过去的就是乱码,请问要怎么解决? 看了网上说要用IntPtr,但是不会使啊,希望大家帮帮忙。
补充:各位,我用的是VB.NET,直接使用String是不能兼容的。
答案:LPCTSTR是个指针类型,在vb.net中调用的时候应该定义为 byref 地址引用
这样在传递string参数的时候会把字符串的地址一intptr 格式传递给DLL
其他:假设你的DLL函数名为:
int myfun(LPCTSTR s);
VB调用这样写:
dim a(0 to 255) as char
myfun a(0)
注意要用第一个元素a(0)代替地址!
也可以试试
dim a(0 to 255) as short 或者 long vb的string应该是bstr类型
用CString好像可以接收 你也太牛逼了!!!你不用MFC,你要用WINDOWS API吧,,,你想不用API连SQL???
吐血,累死你啊!!
好,,给你个C++链接SQL2000的例子!
/*   这是一个程序,只使用C++就可以连接数据库,你编译后要按程序要求配置一下环境也许才可以使用*/   
  /**************************************************   
    *   操作系统   Windows   XP   Professional   SP2   
    *   开发环境   Visual   Studio.NET   2003   
    *   数据库   SQL   Server   2000   
    *   注释   配置环境,可以参考代码中的ConnectionString赋值部分   
    *             另外对于SQL   SERVER的密码和用户名得修改一下程序的用户名和密码   
    *             这个程序用的是SQL   SERVER的默认例子数据库,除了用户名和密码,代码基本不用怎么改   
    *             你多注意一下你的环境比如ODBC呀,数据库的用户名呀,口令呀,还有   
    *             操作系统,数据库,程序,三方面的配合吧。   
    **************************************************/   
  //   BeingConnectionStringCpp   
  #import   "C:\Program   Files\Common   Files\System\ADO\msado15.dll"   \   
          no_namespace   rename("EOF",   "EndOfFile")   
    
  #include   <ole2.h>   
  #include   <stdio.h>   
  #include   <conio.h>   
    
  //   Function   declarations   
  inline   void   TESTHR(HRESULT   x)   ;   
  void   ConnectionStringX();   
  _bstr_t   GetState(int   intState);     
  void   PrintProviderError(_ConnectionPtr   pConnection);   
  void   PrintComError(_com_error   &e);   
    
  ///////////////////////////////////////////////////////////   
  //                                                                                                               //   
  //             Main   Function                                                                         //   
  //                                                                                                               //   
  ///////////////////////////////////////////////////////////   
    
  void   main()   
  {   
          if(FAILED(::CoInitialize(NULL)))   
                  return;   
    
          ConnectionStringX();   
    
          //Wait   here   for   user   to   see   the   output..   
          printf("\nPress   any   key   to   continue...");   
          getch();   
    
          ::CoUninitialize();   
  }   
    
  ///////////////////////////////////////////////////////////   
  //                                                                                                               //   
  //             ConnectionStringX   Function                                               //   
  //                                                                                                               //   
  ///////////////////////////////////////////////////////////   
    
  void   ConnectionStringX()   
  {   
          //   Define   Connection   object   pointers.   
          //   Initialize   pointers   on   define.   
          //   These   are   in   the   ADODB::     namespace   
            _ConnectionPtr   pConnection1   =   NULL;   
            _ConnectionPtr   pConnection2   =   NULL;   
            _ConnectionPtr   pConnection3   =   NULL;   
            _ConnectionPtr   pConnection4   =   NULL;   
    
          //Define   Other   Variables   
          HRESULT     hr   =   S_OK;   
    
          try   
          {   
                  //   Open   a   connection   using   OLE   DB   syntax.   
                  TESTHR(pConnection1.CreateInstance(__uuidof(Connection)));   
                  pConnection1->ConnectionString   =     
                          "Provider='sqloledb';Data   Source='MySqlServer';"   
                          "Initial   Catalog='Pubs';Integrated   Security='SSPI';";   
                  pConnection1->ConnectionTimeout   =   30;   
                  pConnection1->Open("","","",adConnectUnspecified);   
                  printf("cnn1   state:   %s\n",     
                          (LPCTSTR)GetState(pConnection1->State));   
                    
                  //   Open   a   connection   using   a   DSN   and   ODBC   tags.   
                  //   It   is   assumed   that   you   have   create   DSN   'Pubs'   with   a   user   name   as     
                  //   'MyUserId'   and   password   as   'MyPassword'.   
                  TESTHR(pConnection2.CreateInstance(__uuidof(Connection)));   
                  pConnection2->ConnectionString   =   "DSN=Pubs;UID=MyUserId;PWD=MyPassword;";   
                  pConnection2->Open("","","",adConnectUnspecified);   
                  printf("cnn2   state:   %s\n",     
                          (LPCTSTR)GetState(pConnection2->State));   
    
                  //   Open   a   connection   using   a   DSN   and   OLE   DB   tags.   
                  TESTHR(pConnection3.CreateInstance(__uuidof(Connection)));   
                  pConnection3->ConnectionString   =   "Data   Source=Pubs;";   
                  pConnection3->Open("","","",adConnectUnspecified);   
                  printf("cnn3   state:   %s\n",     
                          (LPCTSTR)GetState(pConnection3->State));   
    
                  //   Open   a   connection   using   a   DSN   and   individual   
                  //   arguments   instead   of   a   connection   string.   
                  //   It   is   assumed   that   you   have   create   DSN   'Pubs'   with   a   user   name   as     
                  //   'MyUserId'   and   password   as   'MyPassword'.   
                  TESTHR(pConnection4.CreateInstance(__uuidof(Connection)));   
                  pConnection4->Open("Pubs","MyUserId","MyPassword",adConnectUnspecified);   
                  printf("cnn4   state:   %s\n",     
                          (LPCTSTR)GetState(pConnection4->State));   
          }   
          catch(_com_error   &e)   
          {   
                  //   Notify   user   of   any   errors.   
                  //   Pass   a   connection   pointer   accessed   from   the   Connection.   
                  PrintProviderError(pConnection1);   
                  if(pConnection2)   
                          PrintProviderError(pConnection2);   
                  if(pConnection3)   
                          PrintProviderError(pConnection3);   
                  if(pConnection4)   
                          PrintProviderError(pConnection4);   
                  PrintComError(e);   
          }   
    
          //Cleanup   objects   before   exit.   
          if   (pConnection1)   
                  if   (pConnection1->State   ==   adStateOpen)   
                          pConnection1->Close();   
          if   (pConnection2)   
                  if   (pConnection2->State   ==   adStateOpen)   
                          pConnection2->Close();   
          if   (pConnection3)   
                  if   (pConnection3->State   ==   adStateOpen)   
                          pConnection3->Close();   
          if   (pConnection4)   
                  if   (pConnection4->State   ==   adStateOpen)   
                          pConnection4->Close();   
  }   
    
  ///////////////////////////////////////////////////////////   
  //                                                                                                               //   
  //             GetState   Function                                                                 //   
  //                                                                                                               //   
  ///////////////////////////////////////////////////////////   
    
  _bstr_t   GetState(int   intState)     
  {   
          _bstr_t   strState;     
          switch(intState)     
          {   
                  case   adStateClosed:   
                          strState   =   "adStateClosed";   
                          break;   
                  case   adStateOpen:   
                          strState   =   "adStateOpen";   
                          break;   
                  default:   
                  ;   
          }   
          return   strState;   
  }   
    
  ///////////////////////////////////////////////////////////   
  //                                                                                                               //   
  //             PrintProviderError   Function                                             //   
  //                                                                                                               //   
  ///////////////////////////////////////////////////////////   
    
  void   PrintProviderError(_ConnectionPtr   pConnection)   
  {   
          //   Print   Provider   Errors   from   Connection   object.   
          //   pErr   is   a   record   object   in   the   Connection's   Error   collection.   
          ErrorPtr     pErr   =   NULL;   
    
          if(   (pConnection->Errors->Count)   >   0)   
          {   
                  long   nCount   =   pConnection->Errors->Count;   
    
                  //   Collection   ranges   from   0   to   nCount   -1.   
                  for(long   i   =   0;   i   <   nCount;   i++)   
                  {   
                          pErr   =   pConnection->Errors->GetItem(i);   
                          printf("Error   number:   %x\t%s\n",   pErr->Number,     
                                  (LPCSTR)pErr->Description);   
                  }   
          }   
  }   
    
  ///////////////////////////////////////////////////////////   
  //                                                                                                               //   
  //             PrintComError   Function                                                       //   
  //                                                                                                               //   
  ///////////////////////////////////////////////////////////   
    
  void   PrintComError(_com_error   &e)   
  {   
          _bstr_t   bstrSource(e.Source());   
          _bstr_t   bstrDescription(e.Description());   
    
          //   Print   Com   errors.       
          printf("Error\n");   
          printf("\tCode   =   %08lx\n",   e.Error());   
          printf("\tCode   meaning   =   %s\n",   e.ErrorMessage());   
          printf("\tSource   =   %s\n",   (LPCSTR)   bstrSource);   
          printf("\tDescription   =   %s\n",   (LPCSTR)   bstrDescription);   
  }   
  //   EndConnectionStringCpp 

上一个:vb.net中datagridview和dataset
下一个:vb.net鼠标点击一行,该行就成选中状态(使用的是datagridview控件)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,