Solved

TOUGH:How to call a C function from Access97?

Posted on 2000-03-04
9
215 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!
0
Comment
Question by:JeanTrudel
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2583564
You need to declare your function like this:

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

Cheers,

Raymond.
0
 

Author Comment

by:JeanTrudel
ID: 2583710
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
 
LVL 22

Expert Comment

by:nietod
ID: 2583761
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 22

Expert Comment

by:nietod
ID: 2583772
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
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 2583796
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
 
LVL 22

Expert Comment

by:nietod
ID: 2584040
>> 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
 

Author Comment

by:JeanTrudel
ID: 2584326
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
 
LVL 11

Expert Comment

by:alexo
ID: 2585126
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 2585150
Fortunately I'm married to a psychologist, so I get help for free.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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