PC Review


Reply
Thread Tools Rate Thread

InterOp problem

 
 
Carsten Unterberg
Guest
Posts: n/a
 
      9th Sep 2009
Hi everyone,

i've got a problem with interop.

in this case, i've a c++-application for a mobile device which sends my
other application (.net cf) via SendNotifyMessage a wm_copydata message
with a own structure.

structure in c++

struct sData

{

public:

int id;

char szData[500];


public:

sData()

{

memset(this, NULL, sizeof(sData));

}

public:

void SetData(int iApp, char* pszData)

{


strcpy(szData, pszData);

id = sizeof(szData);

}

};



structure in c#
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential,CharSet
= System.Runtime.InteropServices.CharSet.Unicode)]

public struct sData

{


public System.Int32 id;

[System.Runtime.InteropServices.MarshalAs(
System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=500)]

public sbyte[] szData;

}



sending and receiving works without any problems. but when i try to parse
the incoming data with that:

COPYDATASTRUCT cdata =
(COPYDATASTRUCT)System.Runtime.InteropServices.Marshal.PtrToStructure(e.Message.LParam,
typeof(COPYDATASTRUCT));

sData data =
(sData)System.Runtime.InteropServices.Marshal.PtrToStructure(cdata.lpData,
typeof(sData));

has data.szData not the value which i give him in the c++-application.

maybe there is an error, but i don't know where.



Best Regards,



Carsten Unterberg | Test-Framework

http://test-framework.blogspot.com/




 
Reply With Quote
 
 
 
 
Chris Tacke, MVP
Guest
Posts: n/a
 
      9th Sep 2009
What does the definition of COPYDATASTRUCT look like?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

"Carsten Unterberg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi everyone,
>
> i've got a problem with interop.
>
> in this case, i've a c++-application for a mobile device which sends my
> other application (.net cf) via SendNotifyMessage a wm_copydata message
> with a own structure.
>
> structure in c++
>
> struct sData
>
> {
>
> public:
>
> int id;
>
> char szData[500];
>
>
> public:
>
> sData()
>
> {
>
> memset(this, NULL, sizeof(sData));
>
> }
>
> public:
>
> void SetData(int iApp, char* pszData)
>
> {
>
>
> strcpy(szData, pszData);
>
> id = sizeof(szData);
>
> }
>
> };
>
>
>
> structure in c#
> [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential,CharSet
> = System.Runtime.InteropServices.CharSet.Unicode)]
>
> public struct sData
>
> {
>
>
> public System.Int32 id;
>
> [System.Runtime.InteropServices.MarshalAs(
> System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=500)]
>
> public sbyte[] szData;
>
> }
>
>
>
> sending and receiving works without any problems. but when i try to parse
> the incoming data with that:
>
> COPYDATASTRUCT cdata =
> (COPYDATASTRUCT)System.Runtime.InteropServices.Marshal.PtrToStructure(e.Message.LParam,
> typeof(COPYDATASTRUCT));
>
> sData data =
> (sData)System.Runtime.InteropServices.Marshal.PtrToStructure(cdata.lpData,
> typeof(sData));
>
> has data.szData not the value which i give him in the c++-application.
>
> maybe there is an error, but i don't know where.
>
>
>
> Best Regards,
>
>
>
> Carsten Unterberg | Test-Framework
>
> http://test-framework.blogspot.com/
>
>
>
>


 
Reply With Quote
 
 
 
 
Carsten Unterberg
Guest
Posts: n/a
 
      10th Sep 2009
Hey Chris,

in c# the copydatastruct looks like:

public struct COPYDATASTRUCT

{

public System.Int32 dwData;

public System.Int32 cbData;

public System.IntPtr lpData;

}

When I receive a message, I could read the cbData-member without any
problems...



Best Regards,



Carsten Unterberg|Test-Framework

http://test-framework.blogspot.com/








 
Reply With Quote
 
Chris Tacke, MVP
Guest
Posts: n/a
 
      10th Sep 2009
My bet is that you're getting the wrong address or something. I'd debug it
this way:

in the C++ app dump the address of the structure you're sending.

in the C# app, use the memory viewer to verify that your actual data is at
that address. Then verify that the lpData of COPYDATASTRUCT is pointing to
the same place (not pointing to a pointer to that location). If those are
both true, then I'd just do a Marshal.Copy to get the data over to my app.

Though, personally, I'd not use WM_COPY in the first place.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com



"Carsten Unterberg" <(E-Mail Removed)> wrote in message
news:Ov%(E-Mail Removed)...
> Hey Chris,
>
> in c# the copydatastruct looks like:
>
> public struct COPYDATASTRUCT
>
> {
>
> public System.Int32 dwData;
>
> public System.Int32 cbData;
>
> public System.IntPtr lpData;
>
> }
>
> When I receive a message, I could read the cbData-member without any
> problems...
>
>
>
> Best Regards,
>
>
>
> Carsten Unterberg|Test-Framework
>
> http://test-framework.blogspot.com/
>
>
>
>
>
>
>
>


 
Reply With Quote
 
Carsten Unterberg
Guest
Posts: n/a
 
      10th Sep 2009
Hey Chris,

thanks at first for your fast answer. I am just a newbie in c++ and I have
to get some information from a c++ component and send them to my .netcf
application. for sure, there are several other ways, but for me, the way
with SendNotifyMessage/WM_COPYDATA seems to be the easiest...

I will try it with Marshal.Copy, it's new for me as well.

Best regards,

Carsten Unterberg|Test-Framework
http://test-framework.blogspot.com/



"Chris Tacke, MVP" <ctacke.at.opennetcf.dot.com> schrieb im Newsbeitrag
news:E884EF39-77B8-4883-B92A-(E-Mail Removed)...
> My bet is that you're getting the wrong address or something. I'd debug
> it this way:
>
> in the C++ app dump the address of the structure you're sending.
>
> in the C# app, use the memory viewer to verify that your actual data is at
> that address. Then verify that the lpData of COPYDATASTRUCT is pointing
> to the same place (not pointing to a pointer to that location). If those
> are both true, then I'd just do a Marshal.Copy to get the data over to my
> app.
>
> Though, personally, I'd not use WM_COPY in the first place.
>
> --
>
> Chris Tacke, Embedded MVP
> OpenNETCF Consulting
> Giving back to the embedded community
> http://community.OpenNETCF.com
>
>
>
> "Carsten Unterberg" <(E-Mail Removed)> wrote in message
> news:Ov%(E-Mail Removed)...
>> Hey Chris,
>>
>> in c# the copydatastruct looks like:
>>
>> public struct COPYDATASTRUCT
>>
>> {
>>
>> public System.Int32 dwData;
>>
>> public System.Int32 cbData;
>>
>> public System.IntPtr lpData;
>>
>> }
>>
>> When I receive a message, I could read the cbData-member without any
>> problems...
>>
>>
>>
>> Best Regards,
>>
>>
>>
>> Carsten Unterberg|Test-Framework
>>
>> http://test-framework.blogspot.com/
>>
>>
>>
>>
>>
>>
>>
>>

>



 
Reply With Quote
 
Carsten Unterberg
Guest
Posts: n/a
 
      16th Sep 2009
Hey Chris,

thanks for you help. I tried it times and times without any success :-(
Where was no way to make it work. Later than I tried an other sending
function... Instead of SendNotifyMessage() I used the well know
SendMessage() and bingo.. it works fine. The MSDN documentation says that
just PostMessage() won't work with WM_COPYDATA, but SendNotifyMessage()
won't work also.

Now, everything is fine..

Best regards,

Carsten Unterberg|Test-Framework
http://test-framework.blogspot.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interop.CDO uses Interop.ADODB which has a higher version... muriwai Microsoft C# .NET 7 27th Oct 2011 08:00 PM
Excel 2003 Range problem with .Net Interop Simon Neumann Microsoft Excel Programming 0 14th Feb 2005 04:42 PM
Re: Problem with "For each" loop using .NET Interop and Excel Juan Pablo González Microsoft Excel Programming 0 27th May 2004 06:00 PM
Interop without interop Tamir Khason Microsoft C# .NET 11 1st Dec 2003 07:01 PM
Is there a published COM Interop Wrapper for Interop.MSDASC.dll? Burton G. Wilkins Microsoft ADO .NET 0 10th Nov 2003 02:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:32 PM.