problem in accessing the data

hi all,

coming rite to the question..

well i'm passing  arrays of string from my managed code to my

native code ..

TCHAR* a[20]; //globally declared

int i=0; //global

extern "C" _declspec(dllexport) int passstring(TCHAR* f)

{

      aIdea = f;

 / / other codes to process the data 

     i++;

}

extern "C" _declspec(dllexport) void shownum()

{

     for(int q=0;q<i;q++)
    {
        MessageBox(NULL,a[q],TEXT("prnting_num"),MB_OK);
     }     

}             

i'm passing the members of the array of string one by one to the native code...then i call the "shownum()" function from my managed code and it shows the strings correctly...it means that everything works fine upto this point. " a " is storing the values correctly.       

" now in the native code i've a class (say class A ) declared and one public function "processdata()"  ;  "

now when i'm using the values of "a" in this function it's not working at all . even when i'm printing it in the message box it shows nothing..that simply means "unsigned int short* a[20]" is not initialized at all..why this is so.

some more informatin i must give to you that when i'm declaring other global data and assign some value to it..it s accessible within the function "processdata()" ,showing normal behaviour.

   




Answer this question

problem in accessing the data

  • El Looper

    I've updated the sample to access the strings from inside the native code as well. To load the project you need to install the Windows Mobile 5 for Pocket PC SDK.

  • amit_800

    i've been doin the same thing as u hv told me but still i'm getting the same error..i think it'll be better if i give u the code also............

    string[] strarr=new string[5];

    now i'm storing some data in the string array...

    GCHandle h = GCHandle.Alloc(strarr, GCHandleType.Pinned);

    for(int k=0;k<4;k++)

    {

    j=passstring(strarr[k]);

    x=Convert.ToString(j);

    MessageBox.Show(x,"result_back");

    }

    we have the code that i've used in the unmanaged code in my first post. these are the two code samples..that i felt necessary to mention here. if u need some more code samples, do tell me .



  • Meilssa

    anyone please help....

  • Anand K

    The first and foremost problem you have is that your data passed down from managed code is only pinned for the duration of the call to PassString. Then it becomes unpinned and any pointer you saved becomes invalid. To pin a string explicitly you can use a call:

    GCHandle h = GCHandle.Alloc(str, GCHandleType.Pinned);

    The object will become unpinned either when you call h.Free() or when this handle goes out of scope.



  • s26f84

    sir , it'll work fine..i've seen the source code...and thats what i'm doing.

    it's not opening correctly in my computer. it's giving some error like

    (i)Errror retrieving information from user database .platform not found.(ii)refers to adevice latform that does'nt exist in my datstore.

    but what if ,in NativeDll.cpp we hv a class (say A) and i want to access "arritems[ ]" in one of the functions of that class ..for some processing (say comparing with something or removing all the occurrences of 'a' in the string). In that case will that be accessible to that function.

    sir actually in my problem when i do any processing in the extern functions (like in this project sample we hv "SetItem( ) & GetItem( )" ) it is working fine but whenever i want to access that string arry (in this case it is arritems[ ] ) in the function associated with one declared class in my native code ,it is not showing anything. all the members of that string array shows NULL.



  • Warhorse

    You are pinning the array. The array elements stay unpinned.

  • msuydam

    sir , now i've tried again pinning all the array elements . but i'm really very sorry it's not working at all..i don't know what mistake i've been doing . but it's not working. ok let me tell u that i'm just modifying the receiving sms sample given in the msdn web site .In mapirule.cpp i'm doing all the changes . they hv given the sample code for blocking any sms having "zzz" in the message .

    now what i'm doing is to do it for numbers it's working fine when i hardcode the numbers in the mapirule.cpp but now i'm storing the numbers in the database that i want to block then im retrieving the numbers and storing it in the array then i'm passing this array in the native code ,for which i've given tha sample codes in my previous posts.

    There is a class called CMailRuleClient in mapirule.cpp in which i've the function ProcessMessage( ) there i'm using the values that i've stored in the TCHAR* array "a" ( reference to the first post)  . but now it's not working. hope now u can understand the problem better, that i'm facing.

    http://www.microsoft.com/downloads/details.aspx familyid=98CCF3D4-DB7C-4A7D-A323-53BBDBBE0420&displaylang=en



  • sam-pan

    I don't know if I can answer your question but I can tell you what I have found when passing arrays, because I have done this recently. If you pass in a byte[] from your managed code you can pick it up perfectly as a char* on the native side. If you pass a int[] fom the managed code you can pick it up as a int* on the native side. I've tried other combinations and the don't work too well, so I end up doing a lot of casting. Recently I had Alex Feinman help me out. He even gave me a sample project, which was awfully nice IMO. You can check it out here:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=488534&SiteID=1

    Hope this helps,



  • Anmol

    I've put together a sample that shows how to declare a managed string array and pass it down in a manner that keeps it accessible. It is located at http://www.alexfeinman.com/download.asp doc=pindemo.zip

    After reading your last post I wanted to point out that in WM5 you can intercept SMS directly from managed code using Microsoft.WindowsMobile.Messaging namespace



  • problem in accessing the data