2
string []URL = {"www.facebook.com","www.orkut.com","www.yahoo.com"};
            Int32 result = URL.Length;
            SetPolicyURL( URL,result );

this is my C# code where i am trying to pass the array of string to C++ Dll which is imported like this

     [PreserveSig]
    [DllImport("PawCtrl.dll", SetLastError = true, CharSet = CharSet.Auto,  CallingConvention = CallingConvention.StdCall)]
    public static extern void SetPolicyURL( string []policy, Int32 noURL);

but i am not able to receive it in my c++ DLL .

 PAWCTRL_API void __stdcall SetPolicyURL( char ** URLpolicy, __int32 noURL)
 {

for ( int i = 0; i < noURL; i++)
     {

    URLvector.push_back(URLpolicy[i]);
  }
 }

Please can any one help me how i should pass the function

thanks InAdvance

0

2 Answers 2

1

CharSet = CharSet.Auto is wrong, it must be CharSet.Ansi to be compatible with a char* in native code. Use the debugger with unmanaged debugging enabled if you still have trouble, set a breakpoint on the function's first statement.

Also drop [PreserveSig], it has no meaning here. And SetLastError is very likely to be wrong, that's only typical on Windows api functions.

Sign up to request clarification or add additional context in comments.

Comments

0

Example passing string array from VB.Net to C. Compatible with 32 and 64 bit.

VB.Net:

Private Declare Function main_net32 Lib "MyCLib32.dll" Alias "_main_net@8" (ByVal argc As Int32, ByRef sArgs() As String) As Int32
Private Declare Function main_net64 Lib "MyCLib64.dll" Alias "main_net" (ByVal argc As Int32, ByRef sArgs() As String) As Int32

Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    Dim sArgs() As String = Environment.GetCommandLineArgs
    Dim result As Int32
    If Environment.Is64BitProcess Then
        result = main_net64(sArgs.Length, sArgs)
    Else
        result = main_net32(sArgs.Length, sArgs)
    End If
End Sub

C:

#define DllExport(result) __declspec(dllexport) result __stdcall
DllExport(int) main_net(int argc, char ***argv)
{
    int i;
    for(i=0;i<argc;i++)
        printf("%d: %s",i,(*argv)[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.