Using Global Variable with Excel

Questions about MultiCharts and user contributed studies.
arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Using Global Variable with Excel

Postby arjfca » 11 Jul 2013

Hello

I'm looking for an explanation about why using Global Variable string in Excel is causing erroneous data when it is modified

Excel code. Is it the proper way to declare the function?

When using
GVSetNamedString ("String1", "A test String", If I modify the string, I got erroneous data. The previous value is kept in part

If I use GVSetString(3, "This is my string"). I read back using GV_GetString(3) , a similar problem.

I have no problem at all with all the other Global Variable in either Excel and MC

MC way to define the dll

Code: Select all

DefineDLLFunc: "GlobalVariable.dll", lpstr, "GV_GetNamedString", lpstr, lpstr ;
What is s the "LPSTR"? Is it a special code. Do i need to do something special in the Excel declaration?

As usual, any info appreciated.

Martin

Code in Excel

Code: Select all

Public Declare Function GV_SetNamedString Lib "C:\Program Files (x86)\TS Support\MultiCharts\GlobalVariable.dll" _
(ByVal strElementLoc As String, ByVal intSetValue As String) As Long

Public Declare Function GV_GetNamedString Lib "C:\Program Files (x86)\TS Support\MultiCharts\GlobalVariable.dll" _
(ByVal strElementLoc As String, ByVal strOther As String) As String

Public Declare Function GV_GetString Lib "C:\Program Files (x86)\TS Support\MultiCharts\GlobalVariable.dll" _
(ByVal intElementLoc As Long) As Long

Public Declare Function GV_SetString Lib "C:\Program Files (x86)\TS Support\MultiCharts\GlobalVariable.dll" _
(ByVal intElementLoc As Long, ByVal intSetValue As String) As Long


arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Using Global Variable with Excel

Postby arjfca » 11 Jul 2013

I found this information, but not to resolve it

It seem that Excel use "BSTR" format for it's string while C++ "LSTR". Both are different and it may explain why I got problem. Still, I don't know how to resolve that yet

Martin
Hi All,

Thanks everyone for the replies.

I've been reading some information on the web but I'm somehow confused (actually: a lot).

As far as I've read, the following occurs:

VBA, when calling the DLL, will sendout as input parameters string variables of 'string' type which has the following characteristics (please, correct me if I'm wrong in any of the following statements):

VBA string:

1) is UNICODE (meaning 2 bytes per char)
2) length of the string is located at the start of the variable
3) it ends with zero character

C++ string:

1) is ANSI (meaning 1 byte per char)
2) has no length information (length comes with the information)
3) ends with NULL char

On the other hand, the VBA string type is compatible with the C++ BSTR type. I understand by this that the BSTR type in C++ has the same structure as the VBA type.

So, my question now, if I'm expecting parameters coming from VBA, then the function must return a BSTR. Also, input parameters should be BSTR. So, it makes sense to declare the function as follows:

Code:
?
1
extern "C" __declspec( dllexport ) __stdcall BSTR str_separator(BSTR in_string, BSTR sep_char, int id);
The problem now is that, within the function, I must (and I prefer to) deal with c++ strings (I'm using #include <string>).

How do I convert then a BSTR variable (which comes in as an input parameter) into a c++ string (so I can work with strings within the function) and finally convert it back into BSTR so VBA will interpret it correctly?

I'll keep on reading but any hint is highly appreciated...

Many thanks,

Lucas


Return to “MultiCharts”