TOUGH:How to call a C function from Access97?

I have to write a C function to be called by Access 97. I can make a Win32 DLL with a function having no argument and it works. But as soon as I add an argument to my function (in C and in Access), it does not work any more: I get  a message from ACCESS saying that the calling convention is incorrect (error 49). I tried with an integer argument in access and short in C; I also tried with a boolean argument in Access and bool in C. PLEASE HELP!
JeanTrudelAsked:
Who is Participating?
 
alexoConnect With a Mentor Commented:
I answered similar questions before.  I'll summarizebelow but if you'd like to see a good discussion check:
  http://www.experts-exchange.com/bin/EQ.10205200 (9 points)

There are certain rules that, if followed, will allow your DLL functions to be called from VB code.

1. Only functions with C linkage can be called from VB code.
2. The calling convention must be __stdcall (aka WINAPI).
3. The function name "decoration" must be removed using a DEF file.

In other words, let's say you have a function, MyFunc() that you wish to put into MyCode.DLL and call from VB.

MyCode.cpp will contain the function defined thus:

    extern "C" void WINAPI MyFunc()

MyCode.def will look like:

    LIBRARY MyCode

    EXPORTS
        MyFunc

Note that you MUST use the DEF file and not __declspec(dllexport).

Of course, only global functions may be used (implied by extern "C") and the arguments and return value must be known to VB.
0
 
rwilson032697Commented:
You need to declare your function like this:

extern "C" int __declspec(dllexport) __stdcall FunctionName(int);

Cheers,

Raymond.
0
 
JeanTrudelAuthor Commented:
I tried it and it did'nt work. Maybe I made an error. Please contact me at jean@trudel.com so that I can send you my small files. I hope I can accept your answer then.
Jean
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
nietodCommented:
rwilson is correct--at least in part

Here is something i wrote for another related question.

**********************

C++ "decorates" or "mangles" function names by appending a "code" to the end of the name that expresses the parameters passed to the function.  This is used to impliment function overloading, where 2 or more distinct functions have the same name and different parameters.  (Apparently they have the same name--C++ mangles the names so they don't actually have the same name).  

The function is exported with the mangeled name, so you aren't actually specifying the right name to GetProcAddress().  You can use dumpbin to verify this, you should see the function exported with a mangeled name and that is the name that you woudl have to specify to GetProcAddress()

To fix this problem, you can specify the mangeled name to GetProcAddress(), or--better--declared the function as "extern "C" ".  This dissables name mangeling on the function.
0
 
nietodCommented:
So the extern "C" will be needed as suggested, if you had problems they may be bacause you made a mistake.  Another posibility is that you may still have the standard call decoration on the name.

If you look at the exports of the DLL using dumpbin you will see the names of the exported procedures and you will see the decoration I mentioned.  (It is very ugly.)  When you use extern "C" the decoration should go away--usually.  However sometimes when functions are declared to use the standard call calling convention the standard call decortion still remains.  This is a "@" followed by a number, like a function called Increment() might actually have a name like Increment@4 in the exports.  You have two choices, you can specify this decoration in the code that uses the function or you can remove this decoration too.  To remove this decoration you need to use a module definiton file.  (.def file) that lists the undecorated name of the function in the exports.


Also note, that you don't have to reject an answer just because it wasn't working for you 100%.  If you think you might have missunderstood or if you just need more help you should try to work with the expert, rather than rejecting the question.
0
 
nietodCommented:
>> Note that you MUST use the DEF file
I think it depends on the compiler--or something--because in some cases poeple have not needed to do this.
0
 
JeanTrudelAuthor Commented:
You and nietod have been of grat help. But your explanation helped me the most. That's why I chose your comment first. I was so happy that many experts were ready to help! That got me out of a big problem: thanks a lot to all of you
0
 
alexoCommented:
>> You and nietod have been of grat help.

No wonder - I'm actually nietod's alter ego.  In his childhood he used to blame me for his broken toys, etc.  After so many incidents,  I ceased to be fictional and  took a life of my own.  Now I haunt him on EE.
0
 
nietodCommented:
Fortunately I'm married to a psychologist, so I get help for free.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.