none
c++ cli Get address of typedef function from unmanaged (c++) / 'overloaded-function' RRS feed

  • Question

  • I try to get function form  c++ unmanaged code to c++ cli managed code

    but I get this error overloaded-function .

    the code form c++ source code like this :

    typedef bool (*CBPLUGINCOMMAND)(int argc, char** argv);
    
    bool _plugin_registercommand(int pluginHandle, const char* command, CBPLUGINCOMMAND cbCommand, bool debugonly)

    in my c++ /cli Dll file source code

    in header

    // DotNet.h
    #pragma once
    #include <Windows.h>
    #include "_plugins.h"
    #include <plugin_loader.h>
    #include <stringutils.h>
    #include <threading.h>
    #include <Shlwapi.h>
    #include <console.h>
    #pragma comment(lib, "x64dbg.lib")
    #pragma comment(lib, "x64bridge.lib")
    
    using namespace System;
    
    namespace DotNet {
    	public ref class _plugin
    	{
    	public:		
    		delegate  bool *CBPLUGINCOMMAND_(int argc, char** argv);
    		bool _plugin_registercommand_(int pluginHandle, System::String^ command, CBPLUGINCOMMAND_^ cbCommand, bool debugonly);
    		
    	}; 
    
    }

    in body

    #include "stdafx.h"
    #include "DotNet.h"
    
    #include <msclr/marshal.h>
    //#include <Windows.h>
    
    using namespace System;
    using namespace std;
    using System::Runtime::InteropServices::Marshal;
    #using <Microsoft.VisualBasic.dll>
    
    delegate bool *CBPLUGINCOMMAND_(int argc, char** argv);
    
    
    bool DotNet::_plugin::_plugin_registercommand_(int pluginHandle, System::String^ command, CBPLUGINCOMMAND_^ cbCommand, bool debugonly) //
    {	
    	msclr::interop::marshal_context ctx;
    	const char* converted = ctx.marshal_as<const char*>(command);	
    		
    	System::Runtime::InteropServices::GCHandle gch = System::Runtime::InteropServices::GCHandle::Alloc(cbCommand);
    	IntPtr cx = Marshal::GetFunctionPointerForDelegate(cbCommand);
    	CBPLUGINCOMMAND cbC =(CBPLUGINCOMMAND) cx.ToPointer;
    	bool x = _plugin_registercommand( pluginHandle, converted, cbC,  debugonly);
    	return true;
    }
    
    extern "C" __declspec(dllexport) int main()
    {
    	
    	int ss = int(Microsoft::VisualBasic::Interaction::MsgBox(L"DotNet DLL Main Function", Microsoft::VisualBasic::MsgBoxStyle::OkOnly, L"Test"));
    	//MessageBox(nullptr, L"Test", L"Done", 0);
    }

    C2440    'type cast': cannot convert from 'overloaded-function' to 'CBPLUGINCOMMAND'    DotNet    DotNet.cpp    57    
    here >>> CBPLUGINCOMMAND cbC =(CBPLUGINCOMMAND) cx.ToPointer;




    • Edited by Noor-man Monday, August 21, 2017 12:34 PM
    Monday, August 21, 2017 12:11 PM

Answers

  • Add ‘( )’:

       CBPLUGINCOMMAND cbC = (CBPLUGINCOMMAND)cx.ToPointer( );

    And since the native function returns ‘bool’, not ‘bool *’, then I think you should adjust the delegate:

       delegate bool CBPLUGINCOMMAND_( int argc, char** argv );


    • Edited by Viorel_MVP Monday, August 21, 2017 4:37 PM
    • Marked as answer by Noor-man Tuesday, August 22, 2017 5:13 AM
    Monday, August 21, 2017 4:37 PM

All replies

  • Add ‘( )’:

       CBPLUGINCOMMAND cbC = (CBPLUGINCOMMAND)cx.ToPointer( );

    And since the native function returns ‘bool’, not ‘bool *’, then I think you should adjust the delegate:

       delegate bool CBPLUGINCOMMAND_( int argc, char** argv );


    • Edited by Viorel_MVP Monday, August 21, 2017 4:37 PM
    • Marked as answer by Noor-man Tuesday, August 22, 2017 5:13 AM
    Monday, August 21, 2017 4:37 PM
  • Thanks it fix the errors ,but when I define this in my Class ( vb.net ) to call this Functions I got this

    Dim cbNetTestCommand_ As DotNet._plugin.CBPLUGINCOMMAND_ = New _plugin.CBPLUGINCOMMAND_(AddressOf cbNetTestCommand)

     x._plugin_registercommand_(pluginHandle, "DotNetpluginTestCommand", AddressOf cbNetTestCommand, False)


    Severity    Code    Description    Project    File    Line    Suppression State
    Error    BC31143    Method 'Public Function cbNetTestCommand(argc As Integer, argv As String) As Boolean' does not have a signature compatible with delegate 'Delegate Function _plugins.CBPLUGINCOMMAND(argc As Integer, argv As String()) As Boolean'.    DotNetTest    FunctionCode.vb    24    Active

    and

    CBPLUGINCOMMAND_ appear as first parameter is fine but the other not define 

    Monday, August 21, 2017 5:40 PM
  • <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" data-gr-id="13" id="13">thanks</g> I fix this bug too by <g class="gr_ gr_190 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="190" id="190">make</g> some changes to the <g class="gr_ gr_68 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="68" id="68">declartion</g> of <g class="gr_ gr_85 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="85" id="85">Functions ,</g>

    as I see it not accept nested parameter like <g class="gr_ gr_184 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="184" id="184"><g class="gr_ gr_173 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="173" id="173">array</g> .</g>

    BR

    Tuesday, August 22, 2017 5:15 AM