Forum Discussion

Aurelien's avatar
Aurelien
Contributor
13 years ago

Call function from C++.dll

Hi,



I tried to call a function from C++.dll but without success. Here is my code in VBS:



Options Explicit





Sub TestCallDll





   Dim Def_Environment, Def_DLL, activated, lib

   

   Set Def_Environment = DLL.DefineEnvironment(True)

   Set Def_DLL = Def_Environment.DefineDLL("MetrinoPlatformWin32")

   Call Def_DLL.DefineProc("pft_IsOptionActivated", vt_Int, vt_Int, vt_Int, vt_Int, vt_Int, vt_Bool) 

   Set lib = Def_Environment.Load("C:\Program Files\Common Files\Gerico\Bin\MetrinoPlatformWin32.dll")

   Call lib.pft_IsOptionActivated(0, 0, 0, 15, 1, activated)

   

End Sub


The information provided by the developer in c#:

bool
activated; 


pft_IsOptionActivated(0, 0, 0, 15, 1, out activated);

canRun =
activated;



[DllImport("MetrinoPlatformWin32.dll", EntryPoint =
"pft_IsOptionActivated",
SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]



internal static
extern void
pft_IsOptionActivated(int card, int link, int unit, int slot, int
optionIndex, [MarshalAs(UnmanagedType.I1)] out
bool activated);


My error is at the line: Call lib.pft_IsOptionActivated(0, 0, 0, 15, 1, activated)

Error message: Object doesn't support this property or method: 'lib.pft_IsOptionActivated'



Could someone tell me where I am wrong please?



Thanks and regards



PS: I have a poor experience in programing.

6 Replies

  • Hi,



    You need to define your function's return value as the last parameter of DefineProc. Also, the last bool parameter of your function is an out parameter which must be passed by reference. Use the code below to define your function.

    Call Def_DLL.DefineProc("pft_IsOptionActivated", vt_int, vt_int, vt_int, vt_int, vt_int, vt_byref OR vt_bool, vt_void)

  • Hi Jared and thanks for your answer,



    I have modified my code as you suggested:



    Options Explicit



    Sub TestCallDll 



       Dim Def_Environment, Def_DLL, activated, lib 

        

       Set Def_Environment = DLL.DefineEnvironment(True) 

       Set Def_DLL = Def_Environment.DefineDLL("MetrinoPlatformWin32") 

       Call Def_DLL.DefineProc("pft_IsOptionActivated", vt_int, vt_int, vt_int, vt_int, vt_int, vt_byref OR vt_bool, vt_void) 

       Set lib = Def_Environment.Load("C:\Program Files\Common Files\Gerico\Bin\MetrinoPlatformWin32.dll") 

       Call lib.pft_IsOptionActivated(0, 0, 0, 15, 1, activated) 

        

    End Sub 



    I still have the same error: Object doesn't support this property or method: 'lib.pft_IsOptionActivated'



    I have validated the path to dll was correct and I got confirmation this a non-com dll in C++ by the developer.

    Do I miss or misunderstand something else please?



    Thanks and Regards,



    Aurélien
  • Hi AurÉlien,



    Your script looks correct. Can you send us your DLL so we can check it here? This way, we can find out if there's something wrong with the DLL iself or create a working example of calling functions from it.
  • Hi AurÉlien,



    We've received your DLL, thanks. I'll reply to the support case you've submitted.
  • For the record:



    From Jared:



    "The problem is in the way exported
    functions in your DLL are named. The name of the function you need to call is
    actually _pft_IsOptionActivated@24 in the list of exported functions. Most
    likely, this occurs because of the way this DLL is compiled - it must match the
    stdcall calling convention (as, for example, Windows API functions do). I
    guess, this is something your developers should look into.

    Anyway, currently, to define your function
    in the DLL, you need to use the following code:


    Call Def_DLL.DefineProc("_pft_IsOptionActivated@24",
    vt_int, vt_int, vt_int, vt_int, vt_int, vt_byref OR vt_bool, vt_void)



    Once you do this, the wrapper object
    created for your DLL in TC will have a method named zpft_IsOptionActivated_24.
    This is because some characters (@ in this case) are replaced with underscores
    to create a valid identifier. The underscore the the beginning of the name is
    replaced with a 'z' to make the identifier VBScript-compatible (VBScript
    doesn't allow starting identifiers with underscores).


    To call your function, use the following
    code:


    Call lib.zpft_IsOptionActivated_24(0, 0, 0,
    15, 1, activated)"



    My Code that works now:



    Options Explicit



    Sub TestCallDll 



       Dim Def_Environment, Def_DLL, activated, lib 

        

       Set Def_Environment = DLL.DefineEnvironment(True) 

       Set Def_DLL = Def_Environment.DefineDLL("MetrinoPlatformWin32") 

       Call Def_DLL.DefineProc("_pft_IsOptionActivated@24", vt_int, vt_int, vt_int, vt_int, vt_int, vt_byref OR vt_bool, vt_void) 

       Set lib = Def_Environment.Load("C:\Program Files\Common Files\Gerico\Bin\MetrinoPlatformWin32.dll") 

       Call lib.zpft_IsOptionActivated_24(0, 0, 0, 15, 1, activated)

        

    End Sub 



    Thanks to Jared.



    Aurélien