473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Need Help with COM call



I'm attempting to implement a 3rd party COM library in a C# application,
and have run up against the following problem in my development. I am
trying to use a particular method call of an object, but it looks like
tlbimp has imported the call in a way that I can't figure out how to get
my data in.

SDK:
VARIANT_BOOL ReadMemory(unsigned char* Data, long DataSize);

IL file:
.method public hidebysig newslot virtual instance bool
ReadMemory([in] unsigned int8& Data,
[in] int32 DataSize) runtime managed internalcall
{
.override [Class]::ReadMemory
} // end of method [Class]::ReadMemory

The Data parameter is supposed to be the contents of an image file (JPG,
etc) read into memory, and in my application is declared as byte[]. The
IL call has it as a pointer to a byte. How can I change the IL file to
the correct type so that I can send in a byte[]?

Thanks!

Gabe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
7 1483
Gabe,

I'm a little confused by the original signature, since you have a
VARIANT_BOOL that is returned, but then you have an unsigned char pointer,
which is not automation compliant. If it was automation compliant, then I
would expect a safearray, and if it was not, I'd expect the return type to
be BOOL. If it was declared as a SafeArray, then the interop library
generated would have exposed a byte array.

If you can't change that, then what you will want to do is define your
interface in code, or modify the IL output by the TLBIMP tool (using ILDASM
to get the IL from the assembly, editing it, and then calling ILASM to
recompile it). When you re-define the COM interface, you will want to
expose the unsigned char as an IntPtr. This will cause the pointer to be
passed to your code.

Then, you will have to convert the array to a byte array and copy it
into managed memory. You can do this using the various static methods on
the Marshal class. If you need help with this, I can elaborate.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gabe Covert" <no****@thecoverts.net> wrote in message
news:u%****************@TK2MSFTNGP11.phx.gbl...


I'm attempting to implement a 3rd party COM library in a C# application,
and have run up against the following problem in my development. I am
trying to use a particular method call of an object, but it looks like
tlbimp has imported the call in a way that I can't figure out how to get
my data in.

SDK:
VARIANT_BOOL ReadMemory(unsigned char* Data, long DataSize);

IL file:
method public hidebysig newslot virtual instance bool
ReadMemory([in] unsigned int8& Data,
[in] int32 DataSize) runtime managed internalcall
{
.override [Class]::ReadMemory
} // end of method [Class]::ReadMemory

The Data parameter is supposed to be the contents of an image file (JPG,
etc) read into memory, and in my application is declared as byte[]. The
IL call has it as a pointer to a byte. How can I change the IL file to
the correct type so that I can send in a byte[]?

Thanks!

Gabe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Unfortunately, I can't change it, as it's a third-party COM library...
I've dug into the IL, but being a fairly new C# developer, and with my
C++ skill sbeing fairly rusty, I'm perplexed as to what changes I would
need to make to the IL to get the parameter conversion done correctly.

Any help you can offer would be greatly appreciated!

Gabe
If you can't change that, then what you will want to do is define your
interface in code, or modify the IL output by the TLBIMP tool (using ILDASM to get the IL from the assembly, editing it, and then calling ILASM to
recompile it). When you re-define the COM interface, you will want to
expose the unsigned char as an IntPtr. This will cause the pointer to be passed to your code.

Then, you will have to convert the array to a byte array and copy it
into managed memory. You can do this using the various static methods on the Marshal class. If you need help with this, I can elaborate.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Gabe,

Can you post the IDL that is used for the particular interface (you can
use OLE view to generate it). With that, I can give you a piece of code
used to represent the interface which you can cast to and use.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gabe Covert" <no****@thecoverts.net> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
Unfortunately, I can't change it, as it's a third-party COM library...
I've dug into the IL, but being a fairly new C# developer, and with my
C++ skill sbeing fairly rusty, I'm perplexed as to what changes I would
need to make to the IL to get the parameter conversion done correctly.

Any help you can offer would be greatly appreciated!

Gabe
If you can't change that, then what you will want to do is define your
interface in code, or modify the IL output by the TLBIMP tool (using

ILDASM
to get the IL from the assembly, editing it, and then calling ILASM to
recompile it). When you re-define the COM interface, you will want to
expose the unsigned char as an IntPtr. This will cause the pointer to

be
passed to your code.

Then, you will have to convert the array to a byte array and copy it
into managed memory. You can do this using the various static methods

on
the Marshal class. If you need help with this, I can elaborate.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4
Here is what I find in OLE view:

[helpstring("Reads JPG, BMP, GIF, and TIFF file formats from memory")]
HRESULT _stdcall ReadMemory(
[in] unsigned char* Data,
[in] long DataSize,
[out, retval] VARIANT_BOOL* Success);

If I use the ildasm, I get the following:
.method public hidebysig newslot virtual
instance bool ReadMemory([in] unsigned IntPtr Data,
[in] int32 DataSize) runtime
managed internalcall
{
.override Identix.FaceIt.Media.IDibImage::ReadMemory
} // end of method DibImageClass::ReadMemory

Thanks!

Gabe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Gabe,

You would need to define the interface in C# like this:

[Guid("get guid from OLEVIEW")]
// I'm assuming that the interface derives from IUnknown in COM, in which
case, this attribute is needed.
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface ISomeInterface
{
[retval:MarshalAs(UnmanagedType.VariantBool)]
bool Readmemory(IntPtr Data, int DataSize);
}

Then, when you implement it, you can use the methods on the Marshal
class to convert from the pointer (in Data) to the byte array you need.

If you don't want to marshal the data, you can use unsafe code and take
a pointer to void, and then just access the memory directly (however, you
need the specific permission for unsafe code). This might be a better
solution, since it you won't have to copy the memory from unmanaged to
managed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Gabe Covert" <no****@thecoverts.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Here is what I find in OLE view:

[helpstring("Reads JPG, BMP, GIF, and TIFF file formats from memory")]
HRESULT _stdcall ReadMemory(
[in] unsigned char* Data,
[in] long DataSize,
[out, retval] VARIANT_BOOL* Success);

If I use the ildasm, I get the following:
method public hidebysig newslot virtual
instance bool ReadMemory([in] unsigned IntPtr Data,
[in] int32 DataSize) runtime
managed internalcall
{
.override Identix.FaceIt.Media.IDibImage::ReadMemory
} // end of method DibImageClass::ReadMemory

Thanks!

Gabe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6
Where would I declare this? In my code? I attempted to add a new class
library to my solution, and implement the following, but I get errors
when compiling saying that Guid and InterfaceType could not be found,
and I can't seem to find a namespace to add to the using section...

[Guid("7CA67280-8914-11D2-B814-00104BC60522")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface IDibImageInterface
{
[retval:MarshalAs(UnmanagedType.VariantBool)]
bool ReadMemory(IntPtr Data, int DataSize);
}

Did I do something wrong in the declaration, or where the declaration
goes?

Gabe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7
Gabe,

Those classes are in the System.Runtime.InteropServices namespace.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gabe Covert" <no****@thecoverts.net> wrote in message
news:eN**************@TK2MSFTNGP11.phx.gbl...
Where would I declare this? In my code? I attempted to add a new class
library to my solution, and implement the following, but I get errors
when compiling saying that Guid and InterfaceType could not be found,
and I can't seem to find a namespace to add to the using section...

[Guid("7CA67280-8914-11D2-B814-00104BC60522")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface IDibImageInterface
{
[retval:MarshalAs(UnmanagedType.VariantBool)]
bool ReadMemory(IntPtr Data, int DataSize);
}

Did I do something wrong in the declaration, or where the declaration
goes?

Gabe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
1
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout....
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
4
by: sara | last post by:
i am studying a computer engineering and i started taking programming using C++ since month i have question i think it`s easy for you all *prof.programmer* but it`s bit diffecult for me plzz i...
0
by: Siyodia | last post by:
This is a java program which i need to run facing compilation error Its consuming a third party web service method I have the supported files(folder) which contain necessary class files...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.