Skip to content

Instantly share code, notes, and snippets.

@wylliec
Created June 17, 2016 22:43
Show Gist options
  • Save wylliec/bc68be19eec51ac3b73c39bb3814db4d to your computer and use it in GitHub Desktop.
Save wylliec/bc68be19eec51ac3b73c39bb3814db4d to your computer and use it in GitHub Desktop.
C#/C++ Interop with string passing
#include "backup.h"
namespace Backup
{
int Backup(WCHAR* test) {
return wcslen(test);
}
}
#include <stdexcept>
using namespace std;
namespace Backup
{
extern "C" { __declspec(dllexport) int Backup(WCHAR* test); }
}
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