Game Development Reference
In-Depth Information
compiles the intermediary code into machine code at the time of execution. Unman-
aged code is compiled directly into machine code similar to a C++ compiler. Some of
the benefits from managed code are that it is portable to any machine that has the
.NET CLR installed, and the CLR can even detect the state of the machine to maximize
performance. This managed environment comes at some cost of performance. In addi-
tion, C# uses a garbage-collecting memory manager, meaning that programs are not
responsible for cleaning up memory after themselves, although there are exceptions.
C# cannot load static libraries, only dynamically linked libraries. Any unmanaged
code that you call from C# will have to live inside a DLL. Before you see the guts of
some C# Windows Forms, you need to see how C# gains access to the C++ DLL.
NativeMethods Class
The NativeMethods class declares hooks into the DLL so they can be called from
C#. There are a few ways to go about this, but one of the easiest is to declare a C#
static class and then declare all the C free functions in a manner that C# can call
them.
static class NativeMethods
{
const string editorDllName =
GCC4EditorDLL_2010.dll
;
// Editor Framework
initializing, message processing, rendering, shutdown
[DllImport(editorDllName, CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int EditorMain(
IntPtr instancePtrAddress, IntPtr hPrevInstancePtrAddress,
IntPtr hWndPtrAddress, int nCmdShow, int screenWidth, int screenHeight);
-
[DllImport(editorDllName, CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern void WndProc(
IntPtr hWndPtrAddress, int msg, int wParam, int lParam);
[DllImport(editorDllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void RenderFrame();
[DllImport(editorDllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Shutdown();
[DllImport(editorDllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void OpenLevel([MarshalAs(UnmanagedType.BStr)]
string lFileName);
// Actor accessor functions
[DllImport(editorDllName, CallingConvention = CallingConvention.Cdecl)]
 
 
Search WWH ::




Custom Search