problems in calling a dll function from vba - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

problems in calling a dll function from vba

Hi, im haveing some problems in calling a dll function from vba.
Heres the vba code:

Declare Function GroupFiles Lib "c:Figest_comackup.dll" (ByVal origin As String, ByVal destiny As String) As Integer

Function mostrarmensagem()
Dim a As Integer
a = GroupFiles("c:\Setuplog.txt", "c:\experiencia.fib")
End Function

Heres the C source code for the dll:

#include
#include

int __declspec(dllexport) GroupFiles(char *origin, char *destiny)
{
char caracter;
FILE *orig;
FILE *output;
orig=fopen(origin, "r");
output=fopen(destiny, "w");
if (orig!=NULL)
{
caracter= getc(orig);
while (caracter!=EOF)
{
putc(caracter, output);
caracter=getc(orig);
}
}
return 0;
}

whats goin on?
Thanks.

Comments

  • pingpongpingpong Posts: 937Member
    You are using __declspec(dllexport) to export a function from a DLL, while this is alright for EXE's written Visual C/C++ it's not for every other development platform, as in your case, VBA.

    See, __declspec(dllexport) adds a few symbols to the exported names, so either:

    1. Use the dependency walker to find out the exact name of the function you are exporting (most probably something like "_imp_GroupFiles" or something along that, please check. Then change your VBA declare statement like this:

    Declare Function GroupFiles Lib "c:Figest_comackup.dll" Alias "_imp_GroupFiles" (ByVal origin As String, ByVal destiny As String) As Integer

    BTW, dependancy walker is at "Microsoft Visual StudioCommonToolsDepends.exe" for VC++ 6.

    2. Or, change your C code to output a truly portable DLL. Very easy:

    First change
    [code]int __declspec(dllexport) GroupFiles(char *origin, char *destiny) {}[/code]
    to
    [code]int WINAPI GroupFiles(char *origin, char *destiny) {}[/code]

    And then add a new text file to your project, call it export.def and fill it out like this (; is DEF file comment):
    [code]
    LIBRARY BLA
    ; BLA to the name of your DLL, if your DLL is MYDLL.DLL, BLA should be MYDLL

    DESCRIPTION 'Copyright (c) 2002 by me'
    ; Whatever description you want, will show up in exe/dll header readers

    HEAPSIZE 10000
    ; Standard stuff

    EXPORTS

    GroupFiles @1
    ; Start listing your exported functions here, make sure to give unique
    ; ordinal numbers (i.e. if you want to export another function, make @2 and so on).
    [/code]

    Rebuild the project, you should get your DLL, this time with a clean exported function names (just like Windows own API). Your original VB Declare should work now.


    : Hi, im haveing some problems in calling a dll function from vba.
    : Heres the vba code:
    :
    : Declare Function GroupFiles Lib "c:Figest_comackup.dll" (ByVal origin As String, ByVal destiny As String) As Integer
    :
    : Function mostrarmensagem()
    : Dim a As Integer
    : a = GroupFiles("c:\Setuplog.txt", "c:\experiencia.fib")
    : End Function
    :
    : Heres the C source code for the dll:
    :
    : #include
    : #include
    :
    : int __declspec(dllexport) GroupFiles(char *origin, char *destiny)
    : {
    : char caracter;
    : FILE *orig;
    : FILE *output;
    : orig=fopen(origin, "r");
    : output=fopen(destiny, "w");
    : if (orig!=NULL)
    : {
    : caracter= getc(orig);
    : while (caracter!=EOF)
    : {
    : putc(caracter, output);
    : caracter=getc(orig);
    : }
    : }
    : return 0;
    : }
    :
    : whats goin on?
    : Thanks.
    :

Sign In or Register to comment.