Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLL in C for Visual Basic : parameter passing 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I succeeded to write a DLL In C which is usable in Visual Basic 6. The problem I have deals with the differences in types between Visual basic and C (Eg. a long in Visual basic is an integer in C) Since I know this, the functions which are passing numbers work correctly.
Were it goes wrong is when I want to work with strings. I found out that BSTR is the datatype used for strings in VB. This datatype is usable in C, but I want to get the received variable in a char* variable. How can I do that ?
 
I believe all you need to do is in the function that retrieves the BSTR is

char* my_string = (LPCTSTR)received_bstr;

matt
 
you can use also _variant_t. Is good to. See it in comutil.h John Fill
1c.bmp


ivfmd@mail.md
 
I tried the tip of mat (char* my_string = (LPCTSTR)received_bstr;)
but then I got the error message :

cannot convert from 'const char *' to 'char *'

???
 
Hi John Fill

Thanks for reacting !

Putting (char *) before it didn't created any errors any more BUT ...

When I ask the strlen and return the result to VB, it is wrong ... even when I explicitly pass the stringlenght to the DLL function as a parameter and put and '\0' at the correct end position of the string ...

When I do a strcopy to change the string so that it is changed in VB, the DLL function crashes ...

Any tips ?
 
I had this problem in a DLL i wrote. When I get home tonight I will take a look if i Get a chance.

Matt
 
BSTR is not required to end with '\o'. In basic is the same string, but the first two bytes contain the length. So in a c string you can't put a '\0' but in a bstr you can. John Fill
1c.bmp


ivfmd@mail.md
 
This is what I use. Don't forget to free the returned
TCHAR* when you're done with him.

TCHAR* GetTCHAR(BSTR str)
{
UINT uiLen;
TCHAR* s;

uiLen = SysStringLen(str);
s = (TCHAR*)calloc(uiLen+1,sizeof(TCHAR));

if(s)
{
if(str)
{
#ifdef UNICODE
memcpy(s,str,uiLen*sizeof(TCHAR));
#else
if(WideCharToMultiByte(
CP_ACP,0,str,uiLen,
s,uiLen,NULL,NULL ) == 0)
{
free(s);
s = NULL;
}
#endif
}
}

return s;
}
 
Ho, memoryleak,
how about:

#include<iostream>
#include<comdef.h>
#include<string>
using namespace std;


int main()
{

_bstr_t xxx;
xxx=L&quot;hello &quot;;
xxx+=&quot;world&quot;;
wcout<<(wchar_t*)xxx<<endl;
BSTR y=L&quot;, whole planet&quot;;
xxx+=y;

cout<<(char*)xxx<<endl;
char* z=&quot;, and other planets&quot;;
xxx+=z;
char* xyz=xxx;
cout<<xyz<<endl;
BSTR c=xxx+L&quot;; ATL is fine :)&quot;;
wcout<<c<<endl;
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
Functions in DLL must be &quot;Standard Call&quot; for VB to use

This is DLL in C
#include <windows.h>

long _stdcall CopyVbStr(LPSTR pStr,LPSTR pStr2);

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

/////////////////////////////////////////////

long _stdcall CopyVbStr(LPSTR pStr,LPSTR pStr2)
{
strcpy(pStr2,pStr);
return strlen(pStr);

}


And This is DEF file
LIBRARY PASSVBSTR

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD SINGLE

EXPORTS
CopyVbStr @1


And This is VB call
Private Declare Function CopyVbStr Lib &quot;c:\tmp\passvbstr\debug\passvbstr.dll&quot; (ByVal s As String, ByVal s2 As String) As Long

Private Sub Command1_Click()
Dim ss$
ss = String(256, 0)
ret=CopyVbStr(&quot;Hello, my name is&quot;, ss)
Debug.Print Left(ss,ret)
End Sub


Hope this helps

Jimmy Le
nhan_tiags@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top