DLL Function Arguments problem

Hi all,

I'm trying to work out my first DLL (my first anything in C other than hello world). My intent is to call a DLL function, including arguments, from VB (in which I am quite versed).

I've been able to create a DLL which returns a specified value to the external caller, but I am running into trouble trying to send argument values. I've attempt to read up on the various methods to do so, including __declspec(dllexport), __stdcall, creating .def files (these I have not been able to successfully compile using bloodshed Dev-C++).

This works to return the value of 155 to the external caller:
1
2
3
4
extern "C" __declspec(dllexport) int MyFunc()
{
  return 155;
}


I can add arguments to this function and compile:
1
2
3
4
extern "C" __declspec(dllexport) int MyFunc(int a, int b)
{
  return a + b;
}


... but when I call this from VB I get a "bad DLL calling convention" error (which is generally attributed to arguments not correctly matching the API requirements).

However, I can do the following:
1
2
3
4
5
extern "C" __declspec(dllexport) int MyFunc(int a, int b)
{
  MessageBox(NULL, TEXT("msg"), TEXT("title"), NULL);
  return a + b;
}


... I call this from VB, passing the a and b parameters, and it fails (bad DLL calling convention again) only after displaying the MessageBox. I would feel safe saying that it fails at the first point that one of the function parameters is used.

For the life of me, I cannot seem to figure this one out. I did some reading on MSDN concerning __stdcall, __thiscall and __cdecl, and surmise that there may be some problem with the arguments being placed in the stack? I'm not sure.

Many thanks in advance for any input that could be given on the topic. I've searched for information online, but have found a bunch of examples that I have gotten to work no better than described above, and have not been able to find anything that I can make sense of concerning the passing of arguments to an exposed DLL function.

Cheers,
Jack
After reading another post, I installed Code::Blocks and tried here instead of using Dev-C++

main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__ 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

int DLL_EXPORT GetDiameter(int Radius)
{
    return Radius * 2;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}



When attempting to call from VB I get "Can't find DLL entry point GetDiameter in ...."


It appears that the header is taking care of the DLL_EXPORT, which, as far as I'm guessing, is used in the function declaration to "expose" the function so that it may be called from an external application.

The SomeFunction is placed in main.cpp by CodeBlocks when creating the new dll. I'm quite lost, because taking hints from the boilerplate code provided at the new project, I'm guessing this is supposed to work.

I will keep playing around with it, though I seem to being very new (a day) I seem to be having some trouble understanding the various online help that I can find.

Any input greatly appreciated.

Thanks,
Jack
Sorry to keep posting, but I did have some (little) progress.

I realized that I need to add a prototype of the GetDiameter function in main.h (line 23), which gets rid of my error regarding not being able to find an entry point.

However, still with both functions I am receiving the Bad DLL Convention error. SomeFunction displays the messagebox fine, but I receive the error (on the VB end) afterwords.
Topic archived. No new replies allowed.