Board index » cppbuilder » Windows DLL in BCB5, Variants, etc.

Windows DLL in BCB5, Variants, etc.


2003-10-17 11:41:03 PM
cppbuilder33
I have a DLL with minimal documentation and examples in VB. I have succesfully (I think) used "impdef" and "implib" to create a useable LIB file for linking. ...at least the linker doesn't complain, and the application runs.
I DO get an AV and variant conversion exception though.
Here's the VB function definition:
Public Declare Function DoReadC Lib "api.dll" (ByVal DataPath As String, ByVal BufferLen As Integer, Buffer As Any, ReturnLen As Integer) As Integer
...and the associated prototype in my header:
extern "C"{
__declspec(dllimport) int DoReadC(char *DataPath, int BufSize, Variant *Buf, int *RetSize);
}
...then I do something like this in the code:
void __fastcall TfMain::OnShow(TObject *Sender)
{
int Status;
int ByteCount;
Variant DblVal;
Status = DoReadC("/DataPath", sizeof(double), &DblVal, &ByteCount);
}
Am I totally messed up here? I don't know exactly what the VB code means, so I don't know what the function prototype should be. Any help would be greatly appreciated!
-Terry
 
 

Re:Windows DLL in BCB5, Variants, etc.

Try this... maybe it'll work
void __fastcall TfMain::OnShow(TObject *Sender)
{
int Status;
int ByteCount;
char DblVal[100];
Status = DoReadC("/DataPath", 100, &((Variant) DblVal), &ByteCount);
}
 

Re:Windows DLL in BCB5, Variants, etc.

Nope, that causes an AV as well.
When I use the de{*word*81}, and inspect the DblVal variable after that line has been executed, I get this error message: "Error inspecting '(Variant)DblVal':E2451 Undefined symbol 'Variant' "
-Terry
"Andr?Taffarello" <backwards:rb.racsfu.pmoc@taerdna>wrote:
Quote

Try this... maybe it'll work

void __fastcall TfMain::OnShow(TObject *Sender)
{
int Status;
int ByteCount;
char DblVal[100];

Status = DoReadC("/DataPath", 100, &((Variant) DblVal), &ByteCount);
}




 

{smallsort}

Re:Windows DLL in BCB5, Variants, etc.

OK, I figured out part of the mystery.
The DLL function wants a REFERENCE to a Variant. So, the
correct function prototype is:
extern "C"{
__declspec(dllimport) int DoReadC(char *DataPath,
int BufSize, Variant &Buf, int &RetBytes);
}
Then, when I call it, this works without any problems:
void __fastcall TfMain::OnShow(TObject *Sender)
{
int Status;
int ByteCount;
int &ByteCountRef = ByteCount;
Variant DblVal(double(0));
Status = DoReadC("/DataPath", sizeof(double), DblVal,
ByteCountRef);
// Use DblVal here...
}
That plus the fact that VB "int" == C "short int" and I think I
have my problem solved.
Thanks for the help anyway!
-Terry
 

Re:Windows DLL in BCB5, Variants, etc.

Oops! I didn't update the code to reflect the change from "int" to "short". Here's what the final code looks like that works:
MyAPITest.h
extern "C"{
__declspec(dllimport) short DoReadC(char *DataPath,
short BufSize, Variant &Buf, short &RetBytes);
}
MyAPITest.cpp
void __fastcall TfMain::OnShow(TObject *Sender)
{
short Status;
short ByteCount;
short &ByteCountRef = ByteCount;
Variant DblVal(double(0));
Status = DoReadC("/DataPath", sizeof(double), DblVal,
ByteCountRef);
// Use DblVal, Status, ByteCount here...
}
"tchris" < XXXX@XXXXX.COM >wrote:
Quote

OK, I figured out part of the mystery.

The DLL function wants a REFERENCE to a Variant. So, the
correct function prototype is:

extern "C"{
__declspec(dllimport) int DoReadC(char *DataPath,
int BufSize, Variant &Buf, int &RetBytes);
}

Then, when I call it, this works without any problems:

void __fastcall TfMain::OnShow(TObject *Sender)
{
int Status;
int ByteCount;
int &ByteCountRef = ByteCount;
Variant DblVal(double(0));

Status = DoReadC("/DataPath", sizeof(double), DblVal,
ByteCountRef);

// Use DblVal here...
}

That plus the fact that VB "int" == C "short int" and I think I
have my problem solved.

Thanks for the help anyway!

-Terry