Solved

Using a DLL function getting Bad DLL calling convention

Posted on 1998-11-11
13
272 Views
Last Modified: 2010-04-30
I am trying to use the following function in a DLL I have also supplied my code.

Function in DLL

**********************************************************************************************************
* ResetBCBoard()
*
* Function:     This function is to reset a BC board
*
* Parameters:   Input:  cIesID -- IES module ID to which the BC is connected  
*                       cGroupNum -- Group number on which the BC is in
*               Output: None
*
* Logical Control: Put this function number, data length to be sent and data length expected
*                      into the function structure
*                  Call function BasicBCSetValue() to communicate to the IES
*
* Returns: returned value from function BasicBCSetValue()
*
***********************************************************************************************************/
DLLFUNCTION short ResetBCBoard(unsigned char cIesID, unsigned char cGroupNum)
{
    // declare function structure
    FUNCTION_STRUCT structResetBCBoard;

    structResetBCBoard.cFunctionNumber = 0x092;
    structResetBCBoard.nRequestDataLen = 1;     // size of cGroupNum
    structResetBCBoard.nResponseDataLen = 0;

    //call basic BC set value function
    return (BasicBCSetValue(cIesID, cGroupNum, structResetBCBoard,  NULL));

}
My code

    Private Declare Function ResetBCBoard Lib _
    "K:\lou\api\release\iesctrl.dll" _
    (ByVal cIesID As String, ByVal cGroupNum As String) As Integer


Private Sub Command1_Click()
    x = ResetBCBoard("1", "1")
End Sub
0
Comment
Question by:louy
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1444355
Try declaring the parameters as byte instead of string. The function also must be compiled using stdcall calling convention, you can specify them in the settings of the projects of the dll (Alt+F7 in VC)
0
 

Author Comment

by:louy
ID: 1444356
Tried Byte and that did not work.  I called another function in the DLL that needed no paramaters and it returned a value OK.  I did not write the C program and have limited knowledge of C.  I am still working on it.  Any other suggestions?
0
 

Author Comment

by:louy
ID: 1444357
Tried the stdcall option in the compile which was I think Gz, but got a compile error.
0
 
LVL 1

Expert Comment

by:khaledc
ID: 1444358
Declare two strings variables and  declare x as followed:
Dim str1 as string
Dim str2 as string
Dim x as integer
    str1="1"
    str2="1"
    x = ResetBCBoard("1", "1")
One mistake I could see is non matching variable type between the function and x value, therefore x should have same number of bytes as the function. It can be done by declaring x as the same type as the function.
0
 
LVL 2

Expert Comment

by:chris_a
ID: 1444359
The correct declare is:

Private Declare Function ResetBCBoard Lib _
    "K:\lou\api\release\iesctrl.dll" _
    (ByVal cIesID As Byte, ByVal cGroupNum As Byte) _
    As Integer

If this fails your DLL is compiled wrongly
0
 
LVL 7

Expert Comment

by:tward
ID: 1444360
Try to change your function to the following:

#define NOMANGLE

short NOMANGLE _stdcall
ResetBCBoard(unsigned char cIesID, unsigned char cGroupNum)

I have many DLL's written in C that I use and as far as I know the above is how you have to declare your functions in C.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:louy
ID: 1444361
I am not familiar with C.  I tried making the _stdcall changes and the program would not compile.  I get the following IES.C
K:\Lou\API\IES.C(2784) : error C2373: 'ResetBCBoard' : redefinition; different type modifiers
Error executing cl.exe.

IES.OBJ - 1 error(s), 0 warning(s)

Can you be more specific on the changes to the c program.

I tried changing the parameters to Byte and that did not work.

Any other suggestions anybody.  I hate C...
0
 
LVL 7

Expert Comment

by:tward
ID: 1444362
What C Compiler are you using?  If you Email it to me I can get it to work, send it back to you and post what needed to be changed here...  (tward@msii.com)
0
 
LVL 1

Accepted Solution

by:
prozak earned 200 total points
ID: 1444363
When creating a DLL in C for VB there are several things you need to do. First, if you are compiling a C++ file you must put 'extern "C"' in front of every function that is being called by VB. In addition to that, all functions must be compiled using the _stdcall option. This controls how arguments are passed to the C function.  Also, when I compiled DLLs to use with VB5 and VC5, I had to create a separate .DEF file to export the functions. Now that I have said that on to the DLL.

It sounds like you modified the parameters in the .C file and not in the .H file. (Referring to the "redefinition" error"). What I would suggest is the follows. If you're only trying to pass one character as the parameter, declare you C function like this:

DLLFUNCTION short _stdcall ResetBCBoard(unsigned char clesID, unsigned char cGroupNum)

And declare you're VB function like this:

Declare Function ResetBCBoard Lib "dllname.dll" (ByVal clesID As Byte, ByVal cGroupNum As Byte)

That should work great if you're only passing on character per parameter. If you're trying to pass in strings, you need to do it like this:

DLLFUNCTION short _stdcall ResetBCBoard(char *clesID, char *cGroupNum)

And declare you're VB function like this:

Declare Function ResetBCBoard Lib "dllname.dll" (ByVal clesID As String, ByVal cGroupNum As String)

If you do need to pass a string and not just one character, be sure to modify the declaration of ResetBCBoard in your .H file (probably APNES.H) to reflect the above declaration. That's why you were getting the "redefinition" error. Hope this helps...
0
 
LVL 7

Expert Comment

by:tward
ID: 1444364
What C Compiler are you using?  If you Email it to me I can get it to work, send it back to you and post what needed to be changed here...  (tward@msii.com)
0
 

Author Comment

by:louy
ID: 1444365
We are making progress.  Now my VB error is #453 can't find DLL entry point for ResetBCBoard

I made the following change to the .C and .H and it compiled. like you suggested.
DLLFUNCTION short _stdcall ResetBCBoard(unsigned char cIesID, unsigned char cGroupNum);

Here is my VB code

Option Explicit
    Dim y, z    As Byte
    Dim x As Integer
    'Dim cIesID As String * 1
    'Dim CGroupNum As Byte
    Private Declare Function ResetBCBoard Lib _
    "a:\testAPI\iesctrl.dll" _
    (ByVal cIesID As Byte, ByVal CGroupNum As Byte) As Integer

Private Sub Command1_Click()
    y = 1
    z = 1
    x = ResetBCBoard(y, z)  
    'x = ResetBCBoard("1", "1")
End Sub

0
 
LVL 1

Expert Comment

by:prozak
ID: 1444366
What you need to do now is modify the definition (.DEF) file to export the function from the DLL so it is callable by external programs. You should have a .def file as part of you project if you don't you can create one with note pad.

The format is as follows:

LIBRARY iesctrl
EXPORTS
  ResetBCBoard @1

The "@1" is the ordinal position of the exported function. If you already have exported functions in the .DEF file you can add more lines and just make the position one more than the preceding function. For example

LIBRARY iesctrl
EXPORTS
  some_function_1 @1
  some_other_function @2
  ResetBCBoard @3

That should get rid of your can't find DLL entry point error.
0
 

Author Comment

by:louy
ID: 1444367
that worked Thank you

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now