|
-
September 29th, 2003, 12:31 AM
#1
OCX return a string value to VB
hi
I have a VC++ 6.0 OCX which is used by a VB 6.0 program.
I need to send a string as a argument and have the OCX change
the string's value, how can I do that?
The function looks something like this :
void MyClass::changeStringValue(String_Pointer str1, String_Pointer str2)
{
char *c = "anything 2";
*str1 = "anything";
strcpy(str2, c...
memcpy(str2, c....
//neither copy function worked
}
This method worked for double and long values, it won't work
for strings(i don't know how to declare a String_Pointer in OCX
functions that are visible to VB)
thanks
awni
-
September 29th, 2003, 07:30 AM
#2
Try using a CString as the parameter. CStrings are converted to BSTR (an automation type) in the .odl file of an ActiveX control. Visual Basic should know how to handle a BSTR.
Gort...Klaatu, Barada Nikto!
-
September 30th, 2003, 09:32 AM
#3
no luck
I even tried VARIANT instead of CString , didn't work.
BSTR, LPCSTR didn't either , but LPCSTR accepts strings as
input , BSTR type gives a Type mismatch error.
thanks
awni
-
October 3rd, 2003, 04:52 AM
#4
How about do it the simpler but not as efficient way:
1. VB program pass the string to the ocx (LPCTSTR)
2. ocx return a modified the string to VB program (BSTR)
3. VB program use the returned string to overwrite the original string
-
October 3rd, 2003, 08:27 AM
#5
that works real good for single string returns, like
Code:
BSTR ReturnString()
{
return a_BSTR
}
but if i need to change 2 or more strings like
Code:
void Change2Strings(String_Pointer s1, String_Pointer s2)
{
*s1 = "something";
*s2 = "something else";
}
then I'd need something else...
I wonder if allocating an array of type BYTE, with a single
element and allocating a string immediatly after that like
Code:
dim b(1) as byte
dim s as String
dim s2 as String
I wonder if that would store the string right after the array
in memory.
I can then pass the array to Visual C++. VB passes the address
of the first element, so if the string follows the array in memory, i can just add 1 to the location of the array, and i've got me the address of the first string, i have no idea how to get to the second string though, maybe add 255? (why am i telling you this?)
I'll try it if i get a chance, and let you know(if i get a chance)
thanks
awni
-
October 3rd, 2003, 09:01 AM
#6
Since it's an OCX, you have to use IDL types and you really should use BSTRs, which is the standard string type for COM.
So, in your OCX's idl, declare a function like this:
Code:
// idl
[id(10), helpstring("method ChangeStrings")] HRESULT ChangeStrings([in, out] BSTR *pFirst, [in, out] BSTR *pSecond);
Then in your C++ code, write:
Code:
HRESULT MyOCX::ChangeStrings(BSTR *pFirst, BSTR *pSecond)
{
// Change the strings
BSTR tmp = *pSecond;
// concatenate both strings into pSecond and set pFirst to an empty string
*pSecond = SysAllocStringLen(*pFirst, SysStringLen(*pFirst) + SysStringLen(*pSecond));
wcscpy(*pSecond + SysStringLen(*pFirst), tmp);
SysFreeString(*pFirst);
SysFreeString(tmp);
*pFirst = SysAllocString(0, 0);
return S_OK;
}
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
February 10th, 2004, 07:08 PM
#7
thanks everyone
Yves, where's a good source of information about
passing different types between VB and VC?
thanks
awni
-
February 10th, 2004, 08:56 PM
#8
Check in MSDN under:
Component Development > Microsoft Interface Development Language (midl) > SDK documentation > Interface Definitions and Type Libraries > MIDL data types.
There you have all the raw information. In any case, if you are a bit unsure what type you should use, but you know what it should be in VB, make a VB COM dll with functions that take resp. return these types and inspect the resulting dll in the OLEViewer.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
February 21st, 2004, 06:44 PM
#9
thank you that was very helpful. I managed to pass/return types
I didn't know how to pass/return before. There are still some that
I'm having problems with, such as user defined types. I did see
them in OLEViewer, but I don't know how to make them visible
in both VB and VC. Any ideas?
thanks
awni
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|