|
-
Nov 2nd, 2001, 08:50 AM
#1
Thread Starter
Hyperactive Member
DLL calling procedure
I have a DLL that is written in C. I am trying to use it from VB. I can use most commands, but the most important one is giving me trouble. The problem is in my declaration I am sure. It is causing a GPF... which does not occur in its Visual C++ counterpart calling program. Can anyone help me figure out what is wrong with my VB declare from this????
Here is my VB Declaration:
Private Declare Function mnCheckDevices Lib "mnw.dll" (ByRef mn_DevFlg() As Long, ByRef mn_Port As Long, ByRef mn_DevCnt As Long, ByRef mn_ErrMsg() As Byte, ByRef mn_BuffSize As Long) As Long
Here is the C procedure:
//Inputs:
//dev_flag => pointer to array for active devices
// port => pointer to selected com port
//dev_cnt => pointer to variable for number of devices found
//error_mess => string that holds return error message
//buff_size => pointer to variable for buffer size
//Outputs:
//None
/////////////////////////////////////////////////////////////////////////////
extern "C"
int __declspec(dllexport) WINAPI mnCheckDevices(int *dev_flg, int *port, int *dev_cnt, char *error_mess, int *buff_size)
{
static long start, end;
static int port_num;
static int i, rec_addr[2];
static int errflag[MAX_BOARDS];
static int ret_code;
static int baud_rate;
static bool BaudRatesTried[5];
static char address;
DeviceDialog = new TDeviceDialog(NULL);
DeviceDialog->Show();
DeviceDialog->Update();
port_num = *port;
for (i = 0; i <= MAX_BOARDS; i++)
errflag[i] = dev_flg[i] = 0;
for (i = 0; i < 5; i++)
BaudRatesTried[i] = false;
baud_rate = BaudRates[0];
BaudRatesTried[0] = true;
CloseComm(&port_num); // close comm port
InitComm(&port_num, &baud_rate, buff_size); //reinitialize comm port
while (1)
{
// detect number of devices present on line and their addresses
address = 0x00;
ret_code = XmitPacket("*", &address, &port_num);
if (ret_code < 0)
return ret_code;
ret_code = DeviceDialog->GetActiveDevices(&port_num, rec_addr, error_mess,
dev_flg, dev_cnt);
if (ret_code == 0)
{
chk_devices = true;
break;
}
else if (ret_code < 0)
{
Application->MessageBox("CheckDevices failed.\n\rCommunication error.", NULL, MB_OK);
break;
}
// Select a different baud rate
// BaudRatesTried[port_num-5] = true;
for (i = 0; i < 5; i++)
{
if (!BaudRatesTried[i])
{
baud_rate = BaudRates[i];
BaudRatesTried[i] = true;
break;
}
}
if (i == 5)
{
// DeviceDialog->CloseDialog();
DeviceDialog->Close();
Application->MessageBox("CheckDevices failed. No active devices found.",
NULL, MB_OK);
return -1;
// baud_rate = -1;
// break;
}
else
{
CloseComm(&port_num); // close comm port
InitComm(&port_num, &baud_rate, buff_size); // reinitialize comm port
}
}
if (baud_rate > 0)
{
start = GetTime();
do
{
end = GetTime();
if (end - start < 0)
end = end + 8640000;
} while (end - start < 50); // delay 0.50 seconds
// DeviceDialog->CloseDialog();
DeviceDialog->Close();
}
return baud_rate;
} // mnCheckDevices() ends
"If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"
-
Nov 2nd, 2001, 01:24 PM
#2
Ok.
For the arrays:
Dim arr(10) as Long
Not this: Dim arr() as Long
before you pass it to your dll? Same for the byte array. They have to be allocated from process (VB) memory before you make the call. Plus with the byte array, you can use a string like this
Dim str as String
str = string$(100, chr(0) ) ' this allocates space in the string
instead of your byte array.
the most important thing: all of the things that are pointers in your C code have to get passed ByRef from VB must be existing , already Dim - ed(is this a word?) variables.
GPF's are caused when you have a pointer aimed at nothing.
-
Nov 2nd, 2001, 02:10 PM
#3
Monday Morning Lunatic
Originally posted by jim mcnamara
GPF's are caused when you have a pointer aimed at nothing.
I thought that gave an Access Violation?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
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
|