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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing A UDT to VC DLL and back

Status
Not open for further replies.

MoMad

Programmer
Dec 30, 2001
4
US
Hello All,

I am facing this little problem when I want to pass a UDT to a VC dll and modify it.

I have this:

Code:
// (VC++6.0)

extern "C" {

  typedef struct {
    BYTE id;
    char name[20];
    char desc[28];
    BYTE params;
  } iRec, *LPiRec;

  void LoadUdt( BSTR filename, LPVOID theRec ) {
    LPCSTR f = (LPCSTR) filename;
    LPiRec r = (LPiRec) theRec;

    if ( !f ) return;

    // First clear the record
    memset( r, 0x00, sizeof( iRec ) );

    // Now load the file..
    // CreateFile if not exist
    // Load the contents of the file into theRec via r
    // this is only example
    r->id = fileid;
    r->name = filename;
    // ...

    // thats all
  }

}

// ------------------------------

' VB CODE
Public Sub LoadUdt Lib &quot;<myVc++Lib.dll>&quot; (ByVal filename, ByRef rec as Long)

Is my vb declaration wrong? What do i need to do to make it load record properly?

How would I do that in VB? Any ideas?

Please respond with any ideas you might have. Thanks.

-MoMad
 
Hello?? Anyone here?? Any help?
 
1st thing I would look at is your vbdeclaration when you say byval filename you are passing a &quot;variant&quot;.
C++ does not recognize that as a valid type I would think (without actually having the code its a little hard to test).

Change byval filename to &quot;byval filename as string&quot;
Gilbert M. Vanegas
Programmer Analyst III
County of San Bernardino - ISD
Email : gvanegas@isd.sbcounty.gov
 
Let's not get people thinking C++ knows what a valid Basic type is or isn't.

ByVal passes data, so as far as C++ is concerned, the type is valid when it compiles, VB just needs to fill it in properly. A string in the implied type of Variant versus a one in an implicit string, may not result in the same data being passed. This makes it agood idea to specify the type in your declaration.



Wil Mead
wmead@optonline.net

 
Yes, wil has some good points there.
I took a look at some code I wrote a while back in which vb invokes some functions in a vc dll.

Note that there are many different ways to solve this problem, in C a string is a null-terminated array and visual basics structure is different.

Here is the prototype for a c function that can accept a visual basic string.

int WINAPI _stdcall ExecuteFunction(LPSTR szSQL, LPSTR szError, LPSTR szModule);

Here is the corresponding vb declaration for a call to this c function

Declare Function ExecuteFunction Lib &quot;PIMSCCODE.dll&quot; (ByVal sSql As String, ByVal sError As String, ByVal sModule As String) As Integer

Hope this helps clear the air.
Thanks... Gilbert M. Vanegas
Programmer Analyst III
County of San Bernardino - ISD
Email : gvanegas@isd.sbcounty.gov
 
Is there any good reference anywhere how to write Declare statements based on a given C/C++ function declaration?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top