calling c++ dll from c#

Aug 7 2006 4:02 PM
Hello I have to use a Win32 API in a C# project (VS 2K5). I want to create a API wrapper class in C++. And then use it from my C# code. I tried to use P/Invoke in C# to create a wrapper class, some of the methods didn’t work because Pointers to memory were not working.. QUESTION 1 Now how do I create a wrapper class, save it as dll and use it in C#. Please tell me whole procedure. Because when I try to create a C++ project The VS2005 gives me a list of templates to choose from like “CLR Console Application” “Windows Forms Application”, etc etc Question 2 My project has a method that continuously reads a block of PCM data from an external radio receiver connected to the computer through USB. Is there and way to listen to that signal through C#. IS PCM just like audio signal … if not how do I convert it into audio. ------------------------------------------------------------------------------------------------ I am writing an example of the function I have to write and tell me what type of project I have to make and what procedure I have to follow. #include "stdafx.h" #include #include // G3 API function type declarations typedef int (__stdcall *FNCOpenRadioDevice)(int iDeviceNum); typedef BOOL (__stdcall *FNCCloseRadioDevice)(int hRadio); typedef BOOL (__stdcall *FNCSetAtten)(int hRadio, BOOL fAtten); typedef BOOL (__stdcall *FNCSetPower)(int hRadio, BOOL fPower); typedef BOOL (__stdcall *FNCSetAGC)(int hRadio, int iAGC); typedef BOOL (__stdcall *FNCCodecStart)(int hRadio,void (__stdcall *CallbackFunc)(void *), void *CallbackTarget); typedef BOOL (__stdcall *FNCCodecStop)(int hRadio); typedef unsigned int (__stdcall *FNCCodecRead)(int hRadio,void *Buf,unsigned int Size); typedef struct { int hRadio; FILE *IFFile; FNCCodecRead CodecRead; } CallbackContext; void __stdcall CodecCallback(void *Target) { unsigned int i; char Buf[4096]; CallbackContext *Ctx=(CallbackContext *)Target; while ((i=Ctx->CodecRead(Ctx->hRadio,Buf,sizeof(Buf)))!=0) fwrite(Buf,i,1,Ctx->IFFile); } //int _tmain(int argc, _TCHAR* argv[]) int main(int argc, char* argv[]) { // load the G3 API library HMODULE dll=LoadLibraryA("wrg303api.dll"); if (!dll) {puts("WRG303API.DLL not found !"); return 0;} // link G3 API functions FNCOpenRadioDevice OpenRadioDevice=(FNCOpenRadioDevice)GetProcAddress(dll,"OpenRadioDevice"); FNCCloseRadioDevice CloseRadioDevice=(FNCCloseRadioDevice)GetProcAddress(dll,"CloseRadioDevice"); FNCSetAtten SetAtten=(FNCSetAtten)GetProcAddress(dll,"SetAtten"); FNCSetPower SetPower=(FNCSetPower)GetProcAddress(dll,"SetPower"); FNCSetAGC SetAGC=(FNCSetAGC)GetProcAddress(dll,"SetAGC"); FNCCodecStart CodecStart=(FNCCodecStart)GetProcAddress(dll,"CodecStart"); FNCCodecStop CodecStop=(FNCCodecStop)GetProcAddress(dll,"CodecStop"); FNCCodecRead CodecRead=(FNCCodecRead)GetProcAddress(dll,"CodecRead"); // open the first available radio int hRadio=OpenRadioDevice(0); if (!hRadio) {puts("No WR-G303 device could be opened !");FreeLibrary(dll); return 0;} // set Power, AGC and Attenuator if (SetPower(hRadio,TRUE)) puts("The device is turned ON"); else puts("The device failed to turn ON"); if (SetAtten(hRadio,FALSE)) puts("The attenuator is OFF"); else puts("The attenuator failed to turn OFF"); if (SetAGC(hRadio,3)) puts("The AGC is FAST"); else puts("The AGC failed to switch"); // open the IF samples destination file FILE *f=fopen("IF2.pcm","wb"); // start the IF stream from the codec CallbackContext Ctx; Ctx.hRadio=hRadio; Ctx.IFFile=f; Ctx.CodecRead=CodecRead; if (!CodecStart(hRadio,CodecCallback,&Ctx)) puts("Codec streaming couldn't be started!"); else { puts("Codec streaming properly started!"); while (!ftell(f) ) { } CodecStop(hRadio); fclose(f); } // close the device handle if (CloseRadioDevice(hRadio)) puts("The device is closed properly"); else puts("The device failed to close"); // free the G3 API library FreeLibrary(dll); return 0; }

Answers (1)