Game Development Community

adding c# dll

by Jake london · in Torque 3D Professional · 10/06/2009 (5:24 pm) · 11 replies

I've created a wrapper and I'm currently trying to emded it within torque via COM... unfortunately to no avail.

I'm using the below code but getting all sorts of errors:

CoInitialize(NULL);
Vw::_AdapterPtr adPtr;

HRESULT hRes = adPtr.CreateInstance(Vw::CLSID_Adapter);

if (hRes == S_OK)
{
BSTR *result = new BSTR();
_bstr_t bstr1(_T("xxxxx"));
_bstr_t bstr2(_T("xxxxx"));
adPtr->Login(bstr1, bstr2 ,&result);
}

CoUninitialize ();

I'm striving to get this working.. Please help!

#1
10/06/2009 (11:03 pm)
Unfortunately I'm not able to follow what you're doing here. I thought COM order was CoInitializeEx, CoGetClassObject, CoCreateInstanceEx.

Where does the managed code come in? Is your COM object a C# library?
#2
10/07/2009 (2:56 am)
Hi Brett... yes - my COM obj is a c# library. CoInitEx etc.. is another way to go using __uuidof instead of the class id... there are other methods.


thanks
#3
10/19/2009 (4:06 pm)
OK done.. if anyone needs to know how give me a buzz
#4
10/19/2009 (6:25 pm)
Quote:if anyone needs to know how give me a buzz

It would probably make a nice little resource :)
#5
10/19/2009 (7:58 pm)
Hi Jake:
can you share your solutions? my email is zhangyuejun@yahoo.com
and how is the performence of the c# dll com solution?
Yuejun Zhang
#6
10/19/2009 (9:53 pm)
I'm game, please post this as a resource (can't buzz ya, no buzzer info)
Thanks!
#7
10/20/2009 (3:05 am)
Maybe he means buzz here... *buzz Buzz BUZZ*
#8
10/20/2009 (12:15 pm)
So.. there you go:

1) Create new project in T3D (set as static lib not using ATL) - this is where you will include all calls to COM.
2) follow http://support.microsoft.com/kb/828736
3) I've also replaced a few lines of code from microsoft post. Add below in new project:

HRESULT hr = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);

hr = Test.CreateInstance(__uuidof(MyC#Namespace::MyC#Class));

instead of using __uuidof.

4)Reference new project (Add links to new project to avoid LNK compiler warnings)

#pragma comment(lib, "xxxxxx.lib")
#include "xxxxx.h"

Re performance - I have not stress tested it enough to establish the bottlenecks of COM, however it is way easier and quicker to deploy certain modules this way.. as i've never worked with C++ before.


#9
10/21/2009 (10:33 pm)
Thank you very much :D
#10
07/26/2010 (12:58 am)
Where did you define Test?
#11
08/02/2010 (2:51 am)
Don't know if anyone else is using this post, but I worked out some details on it.

If you follow the directions above and want to pass strings back and forth this is the code to do it.

the c# function is:
public string RegisterPlayer(String SessionKey, String Character_ID, String Zone_ID, String Torque_Client_ID, String Torque_Player_ID, String DataBase_ConnectionString)



the C function header is
const char* TCPMUDObject::RegisterPlayer(const char *SessionKey, const char *Character_ID,const char *Zone_ID,const char *Torque_Client_ID,const char *Torque_Player_ID,const char *DataBase_ConnectionString)

BSTR *result = new BSTR();//Create a BSTR* to hold the string result
HRESULT hr = E_FAIL;
hr = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
IMudEngineFunctionsPtr pIMud(__uuidof(MudConnect::MudEngine));
pIMud->RegisterPlayer( _com_util::ConvertStringToBSTR(SessionKey),
_com_util::ConvertStringToBSTR(Character_ID),
_com_util::ConvertStringToBSTR(Zone_ID),
_com_util::ConvertStringToBSTR(Torque_Client_ID),
_com_util::ConvertStringToBSTR(Torque_Player_ID),
_com_util::ConvertStringToBSTR(DataBase_ConnectionString),
result);
CoUninitialize();
char *buff = Con::getReturnBuffer( 512 );
dSprintf( buff, 512, "%s", _com_util::ConvertBSTRToString(*result) );
delete (result);
return buff;

This will pretty much take care of converting BSTR to Const Char * and vice versa, works pretty good, debugging can be a bit of a pain but it is nice when you are trying to roll a database transaction.

Vince