I'm trying to use a class that has to be in C#, but I also need to compile my file as a C++ dll, because I'm trying to make a Gmod module.
Is there a way to include C# inside a C++ file? Or to use a DLL written in C# from a DLL written in C++?
I made a DLL in C# and a header file to it, but when I tried to compile a C++ dll that includes that header, I got the following error in VC++ 2008:
[quote]MyDLLstuff.obj : error LNK2001: unresolved external symbol "public: void __thiscall SoundSource::Destroy(void)" (?Destroy@SoundSource@@QAEXXZ)[/quote]
Here is my code:
[cpp]
#define WIN32_LEAN_AND_MEAN
// Headers
#include <GMLuaModule.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include "ZonDirectSound.h"
// Module definition
GMOD_MODULE( Init, Shutdown );
// Globals
ILuaInterface* g_Lua;
/*
SoundDevice:
public void init()
public void Destroy()
SoundSource:
public void init(int size, SoundDevice SD)
public void Play(bool loop)
public void Stop()
public void SetPos(int pos)
public void SetFilepath(string file)
public string GetFilepath()
public int[] GetCurPos(int pos)
public int GetWritePos()
public int GetPlayPos()0
public void ClearDescription()
public void SetDescription(bool[] flag, bool[] setting)
public void SetDescriptionFormat(bool[] flag, int[] setting)
public void SetDescriptionSize(int size)
public bool[] GetDescription()
public int[] GetDescriptionFormat()
public int GetDescriptionSize()
public int[] GetData(int Start, int End)
public void SetData(int start, int[] data)
public void SetVolume(int Vol)
public int GetVolume()
public int GetFrequency()
public void SetFrequency(int Freq)
public void Restore()
public bool IsLost()
public bool IsHardware()
public bool IsSoftware()
public bool IsLooping()
public bool IsPlaying()
public bool IsTerminated()
public bool Disposed()
public void Destroy()
*/
class DSS
{
public:
DSS(){source = new SoundSource();}
void Destroy(){source->Destroy();}
private:
SoundSource* source;
};
LUA_FUNCTION( NewDSS )
{
DSS* source = new DSS();
g_Lua->PushLightUserData( source );
return 1;
}
LUA_FUNCTION( DestroyDSS )
{
g_Lua->CheckType( 1, GLua::TYPE_LIGHTUSERDATA );
DSS* source = (DSS*)g_Lua->GetLightUserData( 1 );
source->Destroy();
delete source;
return 0;
}
LUA_FUNCTION( PrintSomething )
{
Msg( "Hello! (DSS 2)\n" );
return 0;
}
// I'll add another function to show you how to use arguments and return something
LUA_FUNCTION( AddNumbers )
{
g_Lua->CheckType( 1, GLua::TYPE_NUMBER ); // arg 1 and 2
g_Lua->CheckType( 2, GLua::TYPE_NUMBER );
float sum = g_Lua->GetNumber( 1 ) + g_Lua->GetNumber( 2 );
g_Lua->Push( sum );
return 1; // We return the amount of values we return, that's one 1 in this case, a number.
}
int Init( lua_State *L )
{
g_Lua = Lua();
// Here we put all the functions in a table, tell me if you don't get something
// ILuaObject is a pointer cointainer thingy for all lua vars? Yep, it kcan hold a table, a number, anything.
// Now let's put it in a neat little table
g_Lua->NewGlobalTable( "DSS" );
ILuaObject* nikita = g_Lua->GetGlobal( "DSS" );
nikita->SetMember( "PrintSomething", PrintSomething );
nikita->SetMember( "AddNumbers", AddNumbers );
nikita->SetMember( "New", NewDSS );
nikita->SetMember( "Destroy", DestroyDSS );
//nikita->SetMember( "Try", Try );
nikita->UnReference(); // Don't forget this, or we'll leak memory
return 0;
}
// Shut down
int Shutdown( lua_State *L )
{
return 0;
}
[/cpp]
Would anyone be so kind as to point me to the right way of dll linking?
I highly doubt the C++ compiler will compile C#.
You could have a look at these 2 modules:
[url]http://www.facepunch.com/showthread.php?t=776324[/url]
[url]http://www.facepunch.com/showthread.php?t=500771[/url]
They are C++ modules which allow to use .net (C#) in Gmod, so you might find something in there for what you need.
[QUOTE=pikzen;22537802]I highly doubt the C++ compiler will compile C#.[/QUOTE]
C# was compiled by SharpDevelop into a dll. When trying to compile a second dll, I have C++ source, a header, and a precompiled dll that it is to link to.
That's not how it works
Use [url=http://www.facepunch.com/showthread.php?t=500771]this[/url] aforementioned module to make .NET plugins for Garry's Mod.
I believe all .NET Lua interfaces are broken now due to the recent removal of all of the lua_ exports. I think it's best to use C++'s implementation of .NET.
[editline]02:47AM[/editline]
As for what you are doing, C# is unable to be included in C++ unless it is exposed through COM.
How can I make the C# DLL I write exposed through COM?
[cpp]
using System.Runtime.InteropServices;
[ComVisible(true)]
public class MyComVisibleClass
{ }
[/cpp]
Thanks, I'll try that.
Sorry, you need to Log In to post a reply to this thread.