Académique Documents
Professionnel Documents
Culture Documents
Introduction
Summary
C# Process Model
using System;
using System.Runtime.InteropServices;
class VirtualMemoryManager {
[DllImport(kernel32.dll,EntryPoint=GetCurrentProcess)
]
internal static extern IntPtr
GetCurrentProcessHandle();
}
class Program {
unsafe static int Main( string[] args ){
//
}
}
Or
class Program {
static int Main( string[] args ){
//
unsafe{
//
}
//
}
}
Need to compile with the switch /unsafe i.e. csc.exe /unsafe ex.cs
Pointers
byte* pData;
Fixed keyword
using
using
using
using
using
System;
System.Collections.Generic;
System.Windows.Forms;
EngMATLib;
System.Runtime.InteropServices;
namespace Mat_Envelope {
static class Program {
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern ushort GlobalAddAtom(string lpString);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern ushort GlobalFindAtom(string lpString);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern ushort GlobalDeleteAtom(ushort atom);
[STAThread]
static void Main() {
string atomStr = Application.ProductName + Application.ProductVersion;
ushort Atom = GlobalFindAtom(atomStr);
if (Atom > 0) {
MessageBox.Show("You are trying to open second instance of the App!");
} else {
// The Atom does not exist. Now we need to add it:
Atom = GlobalAddAtom(atomStr);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MatEnvelope());
GlobalDeleteAtom(Atom);
}
}
}
}
You can get the interface from MATHWORKS File sharing for running matlab from a C# program
Another example
// Marshal.cs
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest {
[DllImport("msvcrt.dll")]
public static extern int puts(
[MarshalAs(UnmanagedType.LPStr)] string m);
[DllImport("msvcrt.dll")]
internal static extern int_flushall();
public static void Main() {
puts("Hello World!");
_flushall();
}
}
Summary
Acknowledgement