Using .net in Torque: practical sample (connection to postgresql)
Last Updated (Tuesday, 20 May 2008 18:28) Written by Antonio Serrano Friday, 25 April 2008 17:01
你好
If you feel comfortable with .net you can use it to do some tasks for you in Torque.
This just a sample of how you can use .net in torque.
All the source code is free and you can use it at your own risk.
If you need more references you can go to MSDN web page or GarageGames web page. Some of this information has been extracted from these web pages.
For this sample I've used Visual C# 2005, Visual c++ 2008 and Mono libraries (ms.2.0) for postgresql connection.
In this sample we're going to obtain only the database version. This sample is similar to do a "select" or "insert".
To do this, first, we create a C# dll (class library).
Edit the source class source code and add a public interface and change its name if you want. This is the interface we'll use to expose methods through COM.
Add too some postegresql functionality, in this case, just a "select version()".
---
using Npgsql;
namespace Database
{
public interface IPGDatabase
{
String pgVersion();
}
public class PGDatabase : IPGDatabase
{
conn = new NpgsqlConnection("Server=192.168.11.150;Port=5432;User Id=user;Password=secret;Database=DBl;");
conn.Open();
NpgsqlCommand command = new NpgsqlCommand("select version()", conn);
String serverversion;
...
---
To expose com interface we must edit the AssemblyInfo.cs file and change the line "[assembly: ComVisible(false)]" by "[assembly: ComVisible(true)]".
We're going now to create a strong name to our DLL. Go to Visual Studio Command prompt and type "snk.exe -k database.snk". Copy this file to your project, edit project properties and go to signing tab. Click on "Sign the assembly" and choose the file created.
Compile the project to create the Managed DLL.
Now It's time to register the assembly information of the dll.
To do this go to the dll directory in a Visual .Net command prompt and type:
regasm database.dll /tlb:database.tlb /codebase
Copy this file to the Torque project.
Create a c++ class in Torque and prepare it to use in Torque console like the sample below (if you have any doubt go to GarageGames web page):
---
class DBVersion: public SimObject
{
...
public:
DECLARE_CONOBJECT( DBVersion );
...
IMPLEMENT_CONOBJECT( DBVersion );
...
ConsoleMethod(DBVersion,getVersion, const char*, 2, 2,"()Returns the postgresql.")
....
---
Import the .tlb file adding the line "#import "..\game\database.tlb" raw_interfaces_only", and use the new namespace just created "using namespace database"
If all has gone well, we can access now to our managed dll.
Just create the interface pointer like the source code below.
---
HRESULT hr=CoInitialize(NULL);
IPGDatabasePtr pIDatabase(__uuidof(PGDatabase));
long lResult=0;
pIDatabase->pgVersion(&bstrstring);
CoUninitialize();
---
Because we're returning a BSTR string we'll need to a little conversion to return the string:
---
_bstr_t _bstrstring(bstrstring);
strcpy_s(version, (char *)_bstrstring);
return (StringTableEntry) &version[0];
---
Time to test it. Compile the project, run the torque engine, open the torque console and type:
$a=new DBVersion();
echo($a.getVersion());
Enjoy it!!
Note that this is only a sample. There is no control errors, we can improve the code...




