P/Invoke of CallBack with char* params: return error




Hi there,

I am trying to P/Invoke a DLL able to perform a wifi scan.

There is a poll method to which I have to pass a pointer to a function. Then
the native code will call back the passed function which has char* parameters.

I tried to write my P/Invoke (see below) but when I run the application, I
get an exception when the "poll" method is called. The exception has the
following message: Attempted to read or write protected memory. This is often
an indication that other memory is corrupt.

As far as I read, I am not sure that my "delegate" is correct. I am also
unsure about how I should translate the "char *" in C#.

Your help (and code snipets) will be greatly appreciated! Thanks a lot!

Lionel Reyero

==========================
Here is the .h I want to P/Invoke


#ifndef __COMMON_SPOTTER_H__
#define __COMMON_SPOTTER_H__


typedef void (*SawAPFunction)(char *bssid, char *ssid, int rss,int wep,int
infrMode);

/* returns 0 on success, -1 on failure */
int spotter_init();

void spotter_shutdown();

/* returns 0 on success, -1 on failure */
int spotter_poll(SawAPFunction fn);


#endif /* __COMMON_SPOTTER_H__ */


===============================
Here is the P/Invoke I have done:

public delegate void CallBack(IntPtr bssid, IntPtr ssid, int rss, int
wep, int infrMode);


class Spotter
{

[DllImport("spotter.dll",
EntryPoint="_Java_org_placelab_spotter_WiFiSpotter_spotter_1init@8")]
public static extern int init();

[DllImport("spotter.dll", EntryPoint =
"_Java_org_placelab_spotter_WiFiSpotter_spotter_1shutdown@8")]
public static extern void shutdown();

[DllImport("spotter.dll", EntryPoint =
"_Java_org_placelab_spotter_WiFiSpotter_spotter_1poll@8")]
public static extern int poll(CallBack x);

public static void AP_detected(IntPtr bssid, IntPtr ssid, int rss,
int wep, int infrMode)
{

}
}
.



Relevant Pages

  • Re: flatten multi-dimensional array to on-dimensional array
    ... I pretty much create an array in C# and send it to C. C# ... In the C level I receive a void* (by 'pure' luck it is ... public static extern void printArr(int[,] A, int I, int J); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is WinCE .Net support "Unsafe" Call?
    ... Chris Tacke, eMVP wrote: ... > P/Invoke is definitely supported, in fact I think it's pretty much required ... public static extern int MyCall ... protected override void Dispose(bool disposing) ...
    (microsoft.public.pocketpc.developer)
  • Marshaling array of a string in a managed callback called from unmanaged code
    ... typedef int (*GetStringArray)(LPSTR * stringArray, ... __declspecvoid SetGetStringArrayFunction(GetStringArray ... public static extern void SetGetStringArrayFunction(Test.GetStringArray ...
    (microsoft.public.dotnet.framework.interop)
  • Re: What is "Cant find PInvoke dll"?
    ... HARDRTLAYER_API int Start ... Your P/Invoke should work the way you typed it. ... However incase I am missing something here is a past from my code that I know works. ...
    (microsoft.public.windowsce.app.development)
  • Re: Install ROOT certificate programmatically
    ... > This should be trivial to P/Invoke ... > extern static uint CertAddEncodedCertificateToStore( ... > int dwCertEncodingType, ... > IntPtr ppCertContext ...
    (microsoft.public.dotnet.framework.compactframework)