in reply to Re^4: How do I call C++ Dll's class Function in perl using Win32::API
in thread How do I call C++ Dll's class Function in perl using Win32::API

C++ mangles (modifies) names for export so that if the DLL contains two (or more) classes that both have a foo() method, they will not clash.

Run the command: dumpbin /exports your.dll to see what the exported entrypoints look like. These are the function/prototype names you would need to supply to Win32::API.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^6: How do I call C++ Dll's class Function in perl using Win32::API
by Anonymous Monk on Mar 12, 2009 at 20:25 UTC
    Also, Win32::API doesn't expose c++ constructor new
Re^6: How do I call C++ Dll's class Function in perl using Win32::API
by anupama (Novice) on Mar 13, 2009 at 05:42 UTC
    Test.dll dump looks like this
    Microsoft (R) COFF/PE Dumper Version 8.00.50727.42
    Copyright (C) Microsoft Corporation. All rights reserved.
    Dump of file Test.dll
    File Type: DLL
    Section contains the following exports for Test.dll
    00000000 characteristics
    49B9DFE4 time date stamp Fri Mar 13 09:54:04 2009
    0.00 version
    1 ordinal base
    4 number of functions
    4 number of names
    ordinal hint RVA name
    1 0 00001000 ??0CDllTest@@QAE@XZ
    2 1 00001010 ??1CDllTest@@QAE@XZ
    3 2 00001020 ??4CDllTest@@QAEAAV0@ABV0@@Z
    4 3 00001030 SayHello
    Summary
    3000 .data
    2000 .rdata
    1000 .reloc
    1000 .rsrc
    9000 .text
    Is this the problem from Perl side
      Is this the problem from Perl side

      Yes. In order to call the functions in your DLL, you need to supply Win32::API with the name of the function to call--but it has to be the name as exported, not the internal C++ method name.

      Because a single class can have multiple methods with the same name but different nos and/or types of parameters, the names have to be "mangled" for export. Encoded within those mangled names is the number and types of the parameters the functions (methods) expect.

      See Name mangling for more info. It doesn't mean that calling C++ methods is impossible with Win32::API--just makes it sufficiently hard that no one will bother unless they absolutely have no choice. Eg. They have a C++ DLL that they must use, but no source.

      You'd be better of investigating Inline::CPP.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Hi, parallely trying to use Inline::CPP also but not successfull.
        Inline.Test.pl
        #!/usr/bin/perl
        use strict;
        use warnings;
        use Inline CPP => Config =>
        AUTO_INCLUDE => '#include "stdafx.h"',
        LIBS => ' -L/D:\Documents and Settings\M1009280\My Documents\Visual Studio 2005\Projects\Test\Test -lTest.lib',
        INC => '-I D:\Documents and Settings\M1009280\My Documents\Visual Studio 2005\Projects\Test\Test\Test.h',
        INC => '-I C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\INCLUDE',
        PREFIX => 'Test',
        ENABLE => 'STD_IOSTREAM',
        BUILD_NOISY =>1;
        use Inline CPP => <<'EOC';
        #include <Test.h>
        int test(int a){
        a *= 2;
        return a;
        }
        EOC
        my $result = int test(2);
        print "$result\n";


        Executing the above script following error appears
        C:\Perl\bin>perl Inline.Test.pl
        Starting Build Preprocess Stage
        Finished Build Preprocess Stage
        Starting Build Compile Stage
        Starting "perl Makefile.PL" Stage
        Note (probably harmless): No library found for y
        Note (probably harmless): No library found for and
        Note (probably harmless): No library found for
        Settings/M1009280/My
        Note (probably harmless): No library found for Documents/Visual
        Note (probably harmless): No library found for Studio
        Note (probably harmless): No library found for 2005/Projects/Test/Test
        Note (probably harmless): No library found for -lTest.lib Writing Makefile for Inline_Test_pl_89e7
        Finished "perl Makefile.PL" Stage
        Starting "make" Stage
        Microsoft (R) Program Maintenance Utility Version 8.00.50727.42
        Copyright (C) Microsoft Corporation. All rights reserved.
        C:\Perl\bin\perl.exe C:\Perl\lib\ExtUtils\xsubpp -typemap C:\Perl\lib\E xtUtils\typemap Inline_Test_pl_89e7.xs > Inline_Test_pl_89e7.xsc && C:\Perl\bin \perl.exe -MExtUtils::Command -e mv Inline_Test_pl_89e7.xsc Inline_Test_pl_89e7. c
        cl -TP -c -IC:/Perl/bin -I C:\Program Files\Microsoft Visual Studio 8\V C\ATLMFC\INCLUDE -nologo -GF -W3 -MD -Zi -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO_S TRICT -DHAVE_DES_FCRYPT -DUSE_SITECUSTOMIZE -DPRIVLIB_LAST_IN_INC -DPERL_IMPLICI T_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX -MD -Zi -DNDEBU G -O1 -DVERSION=\"0.00\" -DXS_VERSION=\"0.00\" "-IC:\Perl\lib\CORE" Inlin e_Test_pl_89e7.c
        Microsoft c1xx : fatal error C1083: Cannot open source file: 'Files\Microsoft': No such fi le or directory Visual
        c1xx : fatal error C1083: Cannot open source file: 'Visual': No such file or dir ectory Studio
        c1xx : fatal error C1083: Cannot open source file: 'Studio': No such file or dir ectory INCLUDE
        c1xx : fatal error C1083: Cannot open source file: '8\VC\ATLMFC\INCLUDE': No such file or directory Inline_Test_pl_89e7.c
        C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xlocale(326) : warning C45 30: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
        C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xlocale(341) : warning C45
        30: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
        C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xlocale(358) : warning C45 30: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
        C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\istream(1075) : warning C4 530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
        C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\INCLUDE\afx.h(24) : fatal error C1189: #error : Building MFC application with /MDd (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MDdGenerating Code...
        NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\cl.EXE"' : return code '0x2' Stop.
        A problem was encountered while attempting to compile and install your Inline
        CPP code. The command that failed was:
        nmake

        The build directory was:
        C:\Perl\bin\_Inline\build\Inline_Test_pl_89e7

        To debug the problem, cd to the build directory, and inspect the output files.

        at Inline.Test.pl line 16
        BEGIN failed--compilation aborted at Inline.Test.pl line 24.
        Since i had problem while linking the VCC++ Header files i had to go for Win32::API approach
        Please let me know if my approach wirting the above Inline_Test.pl script if correct?