We help IT Professionals succeed at work.

TOUGH:How to call a C function from Access97?

JeanTrudel
JeanTrudel asked
on
246 Views
Last Modified: 2010-04-10
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!
Comment
Watch Question

You need to declare your function like this:

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

Cheers,

Raymond.

Author

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

Commented:
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.

Commented:
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.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
>> 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.

Author

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

Commented:
>> 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.

Commented:
Fortunately I'm married to a psychologist, so I get help for free.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.