1

I need to convert a C++ File.exe source into a File.dll . For this I change the Configuration Type of this source into a Dynamic Library (.dll) and Visual Studio build a File.dll instead of File.exe .

Now Im trying to use this File.dll into my C# project . This dll entrypoint is sth like :

   int wmain(int argc, wchar_t* argv[])
    {
        // codes
    }

after this I pass arguments to the File.exe in CMD like :

   File.exe -l  -a "This is first arg !"

But now the things become complicated .I know that using C++ dlls into the C# code is like :

        [DllImport(@"File.dll" , EntryPoint = "wmain")]
    private static extern int wmain(int a , string arg);

My Questions :

1.The below code is wrong cuz I don't how can I declare (wchar_t* argv[]) as a C# variable ?!

2.If I run the below code in C# it gives : 'Unable to find an entry point named 'wmain' in DLL 'File.dll'.' So it seems that I should do sth first to make entrypoint clear for C# . What's that ?!

Update 1 : It seems that this link has the same scenario for my first question but the difference is I use an array and I can't find a class that is same as (wchar_t) in c# .

Community
  • 1
  • 1
Mohammad Sina Karvandi
  • 1,064
  • 3
  • 25
  • 44
  • 1
    [related](http://stackoverflow.com/questions/11879108/how-to-call-a-c-function-from-c-sharp-with-a-wchar-out-parameter)? – Weak to Enuma Elish Sep 14 '15 at 20:29
  • 1
    @JamesRoot it is similar but the difference is my code is an array ! So how can I pass array in the same scenario ?! – Mohammad Sina Karvandi Sep 14 '15 at 20:35
  • I'd recommend updating your question to reference the related question/answer and state why it is different to avoid an accidental _closed as duplicate_. – James Adkison Sep 14 '15 at 20:40
  • [this](http://stackoverflow.com/a/5192091/4756309) answer suggests that you can just call it a pointer to pointer, not an array of pointers. – Weak to Enuma Elish Sep 14 '15 at 20:42
  • I added a potential idea – Weak to Enuma Elish Sep 14 '15 at 21:37
  • You have to tell Visual C++ to export the function, either using a linker module definition file (`.def`) or using `__declspec(dllexport)`. You probably also want to specify the calling convention (`__stdcall` or use the `WINAPI` macro), although you can control that with a compiler switch or tell C# to use the `cdecl` convention. – Ben Voigt Sep 14 '15 at 21:52
  • 1
    Finally, it's probably better to choose a different name, since `wmain` has special meaning to the C++ toolchain. – Ben Voigt Sep 14 '15 at 21:53

1 Answers1

1
[DllImport(@"File.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]

I don't really use C#, but this answer had the above line which might fix the second problem.

In regards to the first problem, you'll have to test this next one to see if it works, but since IntPtr is the same size as a pointer, it should work for wchar_t* argv[]. Maybe. Oh, almost forgot, you might need the unsafe keyword.

public static extern int wmain(int argc, MarshalAs(UnmanagedType.LPArray)] IntPtr[] argv);

I got this idea from here.

Again, I have almost never used C# but hopefully this can help point you in the right direction.

Community
  • 1
  • 1
Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
  • Thank you for your time. You might be right but how can I put wchar_t data as an IntPtr !!!! but anyway thank you again. – Mohammad Sina Karvandi Sep 14 '15 at 21:50
  • 1
    Well you have an array of `IntPtr`, so each `IntPtr` would correspond to a `wchar_t*`. Since `IntPtr` is designed to be an integer large enough to store a pointer, and `reinterpert_cast` from pointer to integer is well defined, I would assume that the `IntPtr` values would be interpreted as `wchar_t*`. Just make sure `wmain` either doesn't use `argv` or you populate the array before passing it, of course. – Weak to Enuma Elish Sep 14 '15 at 21:55