vb.net接受bstr,VB net

vb.net如何与c语言做的dll互相传递字符串

首先是C的DLL(我的项目叫ConsoleApplication2.dll):

专注于为中小企业提供做网站、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业建湖免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

#include Windows.h

// 导出Test函数,供外部使用

extern "C" __declspec(dllexport) BSTR __cdecl Test(LPSTR p);

BOOL WINAPI DllMain(

HINSTANCE hinstDLL,

DWORD fdwReason,

LPVOID lpReserved)

{

switch (fdwReason)

{

case DLL_PROCESS_ATTACH: break;

case DLL_THREAD_ATTACH: break;

case DLL_THREAD_DETACH: break;

case DLL_PROCESS_DETACH: break;

}

return TRUE;

}

BSTR __cdecl Test(LPSTR p)

{

p[0] = 'X';

return SysAllocString((BSTR)p);

}

然后是VB.Net项目:

Imports System.Runtime.InteropServices

Imports System.Text

Module Module1

REM 生成的是ConsoleApplication2.dll,名字与C里面相同

DllImport("ConsoleApplication2.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.Cdecl)

Public Function Test(MarshalAs(UnmanagedType.LPTStr) ByVal str As StringBuilder) As MarshalAs(UnmanagedType.BStr) String

End Function

Sub Main()

Dim str As StringBuilder = New StringBuilder("Hello")

Dim rstr As String = Test(str)

Console.WriteLine(rstr)

Console.ReadKey()

End Sub

End Module

运行结果:

用vb.net做串口通讯程序要调用哪些api函数?

串口API通信函数编程

16位串口应用程序中,使用的16位的Windows API通信函数:

①OpenComm()打开串口资源,并指定输入、输出缓冲区的大小(以字节计)

CloseComm() 关闭串口;

例:int idComDev;

idComDev = OpenComm("COM1", 1024, 128);

CloseComm(idComDev);

②BuildCommDCB() 、setCommState()填写设备控制块DCB,然后对已打开的串口进行参数配置; 例:DCB dcb;

BuildCommDCB("COM1:2400,n,8,1", dcb);

SetCommState(dcb);

③ ReadComm 、WriteComm()对串口进行读写操作,即数据的接收和发送.

例:char *m_pRecieve; int count;

ReadComm(idComDev,m_pRecieve,count);

Char wr[30]; int count2;

WriteComm(idComDev,wr,count2);

16位下的串口通信程序最大的特点就在于:串口等外部设备的操作有自己特有的API函数;而32位程序则把串口操作(以及并口等)和文件操作统一起来了,使用类似的操作。

在MFC下的32位串口应用程序

32位下串口通信程序可以用两种方法实现:利用ActiveX控件;使用API 通信函数。

使用ActiveX控件,程序实现非常简单,结构清晰,缺点是欠灵活;使用API 通信函数的优缺点则基本上相反。

使用ActiveX控件:

VC++ 6.0提供的MSComm控件通过串行端口发送和接收数据,为应用程序提供串行通信功能。使用非常方便,但可惜的是,很少有介绍MSComm控件的资料。

⑴.在当前的Workspace中插入MSComm控件。

Project菜单------Add to Project----Components and Controls-----Registered

ActiveX Controls---选择Components: Microsoft Communications Control,

version 6.0 插入到当前的Workspace中。

结果添加了类CMSComm(及相应文件:mscomm.h和mscomm.cpp )。

⑵.在MainFrm.h中加入MSComm控件。

protected:

CMSComm m_ComPort;

在Mainfrm.cpp::OnCreare()中:

DWORD style=WS_VISIBLE|WS_CHILD;

if (!m_ComPort.Create(NULL,style,CRect(0,0,0,0),this,ID_COMMCTRL)){

TRACE0("Failed to create OLE Communications Control\n");

return -1; // fail to create

}

⑶.初始化串口

m_ComPort.SetCommPort(1); //选择COM?

m_ComPort. SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes

m_ComPort. SetOutBufferSize(512); //设置输入缓冲区的大小,Bytes//

if(!m_ComPort.GetPortOpen()) //打开串口

m_ComPort.SetPortOpen(TRUE);

m_ComPort.SetInputMode(1); //设置输入方式为二进制方式

m_ComPort.SetSettings("9600,n,8,1"); //设置波特率等参数

m_ComPort.SetRThreshold(1); //为1表示有一个字符引发一个事件

m_ComPort.SetInputLen(0);

⑷.捕捉串口事项。MSComm控件可以采用轮询或事件驱动的方法从端口获取数据。我们介绍比较使用的事件驱动方法:有事件(如接收到数据)时通知程序。在程序中需要捕获并处理这些通讯事件。

在MainFrm.h中:

protected:

afx_msg void OnCommMscomm();

DECLARE_EVENTSINK_MAP()

在MainFrm.cpp中:

BEGIN_EVENTSINK_MAP(CMainFrame,CFrameWnd )

ON_EVENT(CMainFrame,ID_COMMCTRL,1,OnCommMscomm,VTS_NONE) //映射ActiveX控件事件

END_EVENTSINK_MAP()

⑸.串口读写. 完成读写的函数的确很简单,GetInput()和SetOutput()就可。两个函数的原型是:

VARIANT GetInput();及 void SetOutput(const VARIANT newValue);都要使用VARIANT类型(所有Idispatch::Invoke的参数和返回值在内部都是作为VARIANT对象处理的)。

无论是在PC机读取上传数据时还是在PC机发送下行命令时,我们都习惯于使用字符串的形式(也可以说是数组形式)。查阅VARIANT文档知道,可以用BSTR表示字符串,但遗憾的是所有的BSTR都是包含宽字符,即使我们没有定义_UNICODE_UNICODE也是这样! WinNT支持宽字符, 而Win95并不支持。为解决上述问题,我们在实际工作中使用CbyteArray,给出相应的部分程序如下:

void CMainFrame::OnCommMscomm(){

VARIANT vResponse; int k;

if(m_commCtrl.GetCommEvent()==2) {

k=m_commCtrl.GetInBufferCount(); //接收到的字符数目

if(k0) {

vResponse=m_commCtrl.GetInput(); //read

SaveData(k,(unsigned char*) vResponse.parray-pvData);

} // 接收到字符,MSComm控件发送事件 }

。。。。。 // 处理其他MSComm控件

}

void CMainFrame::OnCommSend() {

。。。。。。。。 // 准备需要发送的命令,放在TxData[]中

CByteArray array;

array.RemoveAll();

array.SetSize(Count);

for(i=0;iCount;i++)

array.SetAt(i, TxData[i]);

m_ComPort.SetOutput(COleVariant(array)); // 发送数据 }

二 使用32位的API 通信函数:

⑴.在中MainFrm.cpp定义全局变量

HANDLE hCom; // 准备打开的串口的句柄

HANDLE hCommWatchThread ;//辅助线程的全局函数

⑵.打开串口,设置串口

hCom =CreateFile( "COM2", GENERIC_READ | GENERIC_WRITE, // 允许读写

0, // 此项必须为0

NULL, // no security attrs

OPEN_EXISTING, //设置产生方式

FILE_FLAG_OVERLAPPED, // 我们准备使用异步通信

NULL );

我使用了FILE_FLAG_OVERLAPPED结构。这正是使用API实现非阻塞通信的关键所在。

ASSERT(hCom!=INVALID_HANDLE_VALUE); //检测打开串口操作是否成功

SetCommMask(hCom, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型

SetupComm( hCom, 1024,512) ; //设置输入、输出缓冲区的大小

PurgeComm( hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR

| PURGE_RXCLEAR ); //清干净输入、输出缓冲区

COMMTIMEOUTS CommTimeOuts ; //定义超时结构,并填写该结构

…………

SetCommTimeouts( hCom, CommTimeOuts ) ;//设置读写操作所允许的超时

DCB dcb ; // 定义数据控制块结构

GetCommState(hCom, dcb ) ; //读串口原来的参数设置

dcb.BaudRate =9600; dcb.ByteSize =8; dcb.Parity = NOPARITY;

dcb.StopBits = ONESTOPBIT ;dcb.fBinary = TRUE ;dcb.fParity = FALSE;

SetCommState(hCom, dcb ) ; //串口参数配置

上述的COMMTIMEOUTS结构和DCB都很重要,实际工作中需要仔细选择参数。

⑶启动一个辅助线程,用于串口事件的处理。

Windows提供了两种线程,辅助线程和用户界面线程。辅助线程没有窗口,所以它没有自己的消息循环。但是辅助线程很容易编程,通常也很有用。

在次,我们使用辅助线程。主要用它来监视串口状态,看有无数据到达、通信有无错误;而主线程则可专心进行数据处理、提供友好的用户界面等重要的工作。

hCommWatchThread=

CreateThread( (LPSECURITY_ATTRIBUTES) NULL, //安全属性

0,//初始化线程栈的大小,缺省为与主线程大小相同

(LPTHREAD_START_ROUTINE)CommWatchProc, //线程的全局函数

GetSafeHwnd(), //此处传入了主框架的句柄

0, dwThreadID );

ASSERT(hCommWatchThread!=NULL);

⑷为辅助线程写一个全局函数,主要完成数据接收的工作。请注意OVERLAPPED结构的使用,以及怎样实现了非阻塞通信。

UINT CommWatchProc(HWND hSendWnd){

DWORD dwEvtMask=0 ;

SetCommMask( hCom, EV_RXCHAR|EV_TXEMPTY );//有哪些串口事件需要监视?

WaitCommEvent( hCom, dwEvtMask, os );// 等待串口通信事件的发生

检测返回的dwEvtMask,知道发生了什么串口事件:

if ((dwEvtMask EV_RXCHAR) == EV_RXCHAR){ // 缓冲区中有数据到达

COMSTAT ComStat ; DWORD dwLength;

ClearCommError(hCom, dwErrorFlags, ComStat ) ;

dwLength = ComStat.cbInQue ; //输入缓冲区有多少数据?

if (dwLength 0) { BOOL fReadStat ;

fReadStat = ReadFile( hCom, lpBuffer,dwLength, dwBytesRead,READ_OS( npTTYInfo ) ); //读数据

注:我们在CreareFile()时使用了FILE_FLAG_OVERLAPPED,现在ReadFile()也必须使用

LPOVERLAPPED结构.否则,函数会不正确地报告读操作已完成了.

使用LPOVERLAPPED结构, ReadFile()立即返回,不必等待读操作完成,实现非阻塞

通信.此时, ReadFile()返回FALSE, GetLastError()返回ERROR_IO_PENDING.

if (!fReadStat){

if (GetLastError() == ERROR_IO_PENDING){

while(!GetOverlappedResult(hCom,READ_OS( npTTYInfo ), dwBytesRead, TRUE )){

dwError = GetLastError();

if(dwError == ERROR_IO_INCOMPLETE) continue;//缓冲区数据没有读完,继续

…… ……

::PostMessage((HWND)hSendWnd,WM_NOTIFYPROCESS,0,0);//通知主线程,串口收到数据}

所谓的非阻塞通信,也即异步通信。是指在进行需要花费大量时间的数据读写操作(不仅仅是指串行通信操作)时,一旦调用ReadFile()、WriteFile(), 就能立即返回,而让实际的读写操作在后台运行;相反,如使用阻塞通信,则必须在读或写操作全部完成后才能返回。由于操作可能需要任意长的时间才能完成,于是问题就出现了。

非常阻塞操作还允许读、写操作能同时进行(即重叠操作?),在实际工作中非常有用。

要使用非阻塞通信,首先在CreateFile()时必须使用FILE_FLAG_OVERLAPPED;然后在 ReadFile()时lpOverlapped参数一定不能为NULL,接着检查函数调用的返回值,调用GetLastError(),看是否返回ERROR_IO_PENDING。如是,最后调用GetOverlappedResult()返回重叠操作(overlapped operation)的结果;WriteFile()的使用类似。

⑸.在主线程中发送下行命令。

BOOL fWriteStat ; char szBuffer[count];

…………//准备好发送的数据,放在szBuffer[]中

fWriteStat = WriteFile(hCom, szBuffer, dwBytesToWrite,

dwBytesWritten, WRITE_OS( npTTYInfo ) ); //写数据

//我在CreareFile()时使用了FILE_FLAG_OVERLAPPED,现在WriteFile()也必须使用LPOVERLAPPED结构.否则,函数会不正确地报告写操作已完成了.

使用LPOVERLAPPED结构,WriteFile()立即返回,不必等待写操作完成,实现非阻塞 通信.此时, WriteFile()返回FALSE, GetLastError()返回ERROR_IO_PENDING.

int err=GetLastError();

if (!fWriteStat) {

if(GetLastError() == ERROR_IO_PENDING){

while(!GetOverlappedResult(hCom, WRITE_OS( npTTYInfo ),

dwBytesWritten, TRUE )) {

dwError = GetLastError();

if(dwError == ERROR_IO_INCOMPLETE){// normal result if not finished

dwBytesSent += dwBytesWritten; continue; }

......................

//我使用了多线程技术,在辅助线程中监视串口,有数据到达时依靠事件驱动,读入数据并向主线程报告(发送数据在主线程中,相对说来,下行命令的数据总是少得多);并且,WaitCommEvent()、ReadFile()、WriteFile()都使用了非阻塞通信技术,依靠重叠(overlapped)读写操作,让串口读写操作在后台运行。

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

你也太牛逼了!!!你不用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接受bstr,VB net
文章网址:http://pwwzsj.com/article/hdosgh.html