Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calling C++ dll from C# - how to debug?
Message
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01655067
Message ID:
01656180
Views:
82
Likes (1)
I think for .NET you want to use a StringBuilder as a parameter for non-fixed string buffers.

Go to PInvoke.net and look around for some wrappers that return strings and you can copy the logic using StringBuilder declarations.

+++ Rick ---

>Hi Rick,
>
>Actually, that C++ dll works fine from VFP. But when I tried to call it from C# using DllImport attribute as I shown it crashed C# application. I already determined that there is some strange corruption in that C++ dll, because the older version of it worked.
>
>>You can't call a DLL function that returns a string because there's no way for FoxPro - at least not without an FLL in between - to capture the pointer that the function returns. CDecl calling convention generally doesn't work - you can only really call DLLs with WINAPI calling convention which has all APIs pretty much returning numeric values and any 'real' return values returned in passed in buffers.
>>
>>Your best bet is to wrap the function in a .NET DLL and call it with wwDotnetBridge or plain COM interop.
>>
>>+++ Rick ---
>>
>>>Hi everybody,
>>>
>>>This is how the function is declared in C# (see VFP declaration in a comment - it works fine from VFP):
>>>
>>>
>>> //declare string Invoke1 in (addbs(justpath(.cProgramPath)) + "ww_local.dll") as ww_localInvoke string cInvokeString
>>>
>>>        [DllImport(@"C:\Program Files (x86)\Siriusware\Pool\ww_local.dll", 
>>>            ExactSpelling = false, 
>>>            CallingConvention = CallingConvention.Cdecl, 
>>>            CharSet = CharSet.Auto)]
>>>        static extern String Invoke1([MarshalAs(UnmanagedType.LPTStr)] String str);
>>>
>>>And then in the C# code I try to do
>>>
>>>
>>> translate = Invoke1(invoke);
>>>
>>>and then the C# test stops and I see a dmp file generated.
>>>
>>>If I try to open that file in VS 2015 and debug in mixed mode I get
>>>
>>>Unhandled exception at 0x777F9D71 (ntdll.dll) in vstest.executionengine.x86.exe.13284.dmp: 0xC0000374: A heap has been corrupted (parameters: 0x7782D8D0).
>>>
>>>What can I do to figure out what may be a problem and why it works from VFP but not C#?
>>>
>>>Thanks a lot in advance.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Reply
Map
View

Click here to load this message in the networking platform