Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / May 2006

Tip: Looking for answers? Try searching our database.

Passing byte[] parameter (c#) as unsigned char* (C++) into interop function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
harishashim@gmail.com - 25 May 2006 11:40 GMT
Hello all.. will appreciate all the help I can get here.... thanks in
advance.

I am using one C++ COM dll under interop in C#. Since this lib is not
supposed to be run in C# I really have no venue for support. And I have
no control on the C++ dll side.

This is the C++ function protype I dig from documentation.

VARIANT_BOOL ReadMemory ( unsigned char* Data, long Size);

C# intellisense show this as

bool ReadMemory (ref byte Data, int Size);

This is what I am trying to do:

byte[] byteImage = (byte[]) SomeObject.Image;    //image is basically
raw bmp data
ReadMemory(ref byteImage, byteImage.Length);  //here is the problem

the Error is
Argument '1': cannot convert from 'byte[]' to 'ref byte'

Any input regarding this is appreciated. My limitation is that the
image can be quite big ... upto 30KB. And i need to do this with good
performace in mind. So things like converting to string first (I dont
know hoe to do this too) will help but will result to bad performance.

Thank you sir!!!!
Mattias Sjögren - 25 May 2006 16:11 GMT
>This is the C++ function protype I dig from documentation.
>
[quoted text clipped - 3 lines]
>
>bool ReadMemory (ref byte Data, int Size);

You can modify the interop assembly so the first parameter's type
becomes a byte[] instead and just pass in the array you want. See the
section "Conformant C-Style Arrays" at

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeditinginteropassembly.asp

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

harishashim@gmail.com - 26 May 2006 03:37 GMT
Wow! I don't even know that this can be done. I learn a new thing
today!!! Thanks so much.

This is the IL before and After

BEFORE
=======

.method public hidebysig newslot virtual
           instance bool  ReadMemory([in] unsigned int8& Data,
                                     [in] int32 DataSize) runtime
managed internalcall

AFTER
=====

.method public hidebysig newslot virtual
           instance bool  ReadMemory([in] unsigned int8[] marshal([])
Data,
                                     [in] int32 Size) runtime managed
internalcall

Just after changing and recompile back into interop.xxx.dll, I stumble
a bit since everytime I rebuild the project the interop.xxx.dll is
revert back to its original form. This is probably because VS .NET
recompile the interop dll from original COM dll at every rebuild.

My (temporary) work around to this is to

1. Delete the original refference to the COM dll in refferences part of
explorer window
2. Copy the corrected interop.xxx.dll into output folder (bin\debug)
3. Add interop.xxx.dll refference into my project.

This workaround smell fishie though. I can see a problem already when
building a RELEASE build. I am sure there is a better way to do this.
For instance tell VS.Net to recompile the IL file instead of the
original COM dll.

Further help and advise is appreciated.
Mattias Sjögren - 26 May 2006 19:20 GMT
>This workaround smell fishie though.

There's nothing wrong with it, except perhaps that you should place
the assembly somewhere other than in the bin\debug directory.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.