using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Runtime.InteropServices;
namespace
UseCppDll
{
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public
struct
Bar
{
/// int
public
int
id;
/// char*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public
string
name;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public
struct
Foo
{
/// int
public
int
id;
/// char*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public
string
name;
}
class
Program
{
[DllImport(
"CppDll.dll"
,EntryPoint=
"Add"
)]
extern
static
int
Add(
int
a,
int
b);
[DllImport(
"CppDll.dll"
, EntryPoint =
"GetBar"
,CharSet=CharSet.Ansi)]
extern
static
void
GetBar(
ref
Bar bar);
[DllImport(
"CppDll.dll"
, EntryPoint =
"GetFoo"
, CharSet = CharSet.Ansi)]
extern
static
void
GetFoo(
ref
Foo foo);
static
void
Main(
string
[] args)
{
Console.WriteLine(Add(5,4));
Bar b =
new
Bar();
GetBar(
ref
b);
Console.WriteLine(
"b's id is "
+ b.id);
Console.WriteLine(
"b's name is "
+ b.name);
Foo f =
new
Foo();
GetFoo(
ref
f);
Console.WriteLine(
"f's id is "
+ f.id);
Console.WriteLine(
"f's name is "
+ f.name);
Console.Read();
}
}
}