VBA / Excel和C ++ DLL,具体是字符串的问题 [英] VBA/Excel, and C++ DLL, specifically problems with strings

查看:212
本文介绍了VBA / Excel和C ++ DLL,具体是字符串的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Excel和Arduino之间的串行通信项目。以下是我在VBA中从Arduino读取数据的地方,这是我的问题所在。

 私有声明函数readFromSerialPort LibC:PathToDll.dll(ByRef Buffer As String)As String 



  BigData.Range(Buf)Value =B

另一个名为dataWindow的单元格在Buf中作为参数,所以它在调用时更新。

  = readFromSerialPort(Buf)

这是DLL中另一端的C ++代码。

  DLL_EXPORT BSTR WINAPI readFromSerialPort(LPBSTR bufferTemp){
char errorMsg [] =read failed;
char mbstring [MAX_STRING_SIZE];
BSTR wcstring = * bufferTemp;
int sLen = wcstombs(mbstring,wcstring,MAX_STRING_SIZE);

char charString [MAX_STRING_SIZE];
DWORD dwBytesRead = 0;

if(hSerial == INVALID_HANDLE_VALUE){
ErrorExit(ReadFile(port not open));
int wLen2 = mbstowcs(* bufferTemp,errorMsg,strlen(errorMsg));
return * bufferTemp;
}

if(!ReadFile(hSerial,charString,sLen,& dwBytesRead,NULL)){
ErrorExit(ReadFile);
int wLen2 = mbstowcs(* bufferTemp,errorMsg,strlen(errorMsg));
return * bufferTemp;
}

int wLen2 = mbstowcs(* bufferTemp,charString,sLen);
return * bufferTemp;
}

问题是,当从单元格调用时,这可以工作,但是当我更改它在VBA中声明一个字符串并从那里调用读取。

解决方案

GSerg已经解决了这个问题。我不明白VBA会自动转换为String,因此在DLL中,我不是处理BSTR,而是使用ASCII char *或LPSTRs。此外,由于excel中的错误,从单元格内调用函数不会进行此转换。


I am working on a project for serial communications between Excel and an Arduino. The following is what I have in VBA for the reading of data from the Arduino which is where my problem lies.

Private Declare Function readFromSerialPort Lib "C:PathToDll.dll"(ByRef Buffer As String) As String

And in a looping function within my VBA I have the following which sets a cell value to a string which is my buffer.

BigData.Range("Buf").Value = "B                                            "

Another cell called dataWindow takes in Buf as an argument so it updates when this is called.

=readFromSerialPort(Buf)

And here is the C++ code on the other end in the DLL.

DLL_EXPORT BSTR WINAPI readFromSerialPort(LPBSTR bufferTemp) {
char errorMsg[] = "read failed";
char mbstring[MAX_STRING_SIZE];
BSTR wcstring = *bufferTemp;
int sLen = wcstombs(mbstring,wcstring,MAX_STRING_SIZE);

char charString[MAX_STRING_SIZE];
DWORD dwBytesRead = 0;

if (hSerial == INVALID_HANDLE_VALUE) {
    ErrorExit("ReadFile (port not open)");
    int wLen2 = mbstowcs(*bufferTemp,errorMsg,strlen(errorMsg));
    return *bufferTemp;
}

if(!ReadFile(hSerial, charString, sLen, &dwBytesRead, NULL)) {
    ErrorExit("ReadFile");
    int wLen2 = mbstowcs(*bufferTemp,errorMsg,strlen(errorMsg));
    return *bufferTemp;
}

int wLen2 = mbstowcs(*bufferTemp,charString,sLen);
return *bufferTemp;
}

The issue is that this works when called from a cell but not when I change it to declaring a string in VBA and calling the read from there.

解决方案

GSerg has solved this issue. I had not understood that VBA automatically converts when passing as String, so that in the DLL I am not dealing with BSTRs but with ASCII char* or LPSTRs. Also that due to a bug in excel, calling functions from within a cell does not do this conversion.

这篇关于VBA / Excel和C ++ DLL,具体是字符串的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆