Created
June 17, 2016 22:43
-
-
Save wylliec/bc68be19eec51ac3b73c39bb3814db4d to your computer and use it in GitHub Desktop.
C#/C++ Interop with string passing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "backup.h" | |
namespace Backup | |
{ | |
int Backup(WCHAR* test) { | |
return wcslen(test); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdexcept> | |
using namespace std; | |
namespace Backup | |
{ | |
extern "C" { __declspec(dllexport) int Backup(WCHAR* test); } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Runtime.InteropServices; | |
namespace TestInterop | |
{ | |
class Program | |
{ | |
[DllImport(@"C:\Users\Administrator\workspace\3rdmantools\SQLSnapManager\backup\x64\Debug\backup.dll", | |
CallingConvention = CallingConvention.Cdecl)] | |
public static extern int Backup([MarshalAs(UnmanagedType.LPWStr)] StringBuilder test); | |
static void Main(string[] args) | |
{ | |
StringBuilder testStr = new StringBuilder(1024); | |
testStr.Append("asdf"); | |
Console.WriteLine(Backup(testStr)); // 4 | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment