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 / October 2005

Tip: Looking for answers? Try searching our database.

Calling COM method returning SAFEARRAY(BYTE)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Graham Morris - 05 Oct 2005 14:25 GMT
Googling hasn't solved this little problem, so here goes:

I have a function in C++ which returns a safearray of bytes.  I cannot
change this function, and I want to call it from C++. The IDL
looks like this:

interface IHash : IDispatch
{
   [id(1)] HRESULT DoHash([in] BSTR strSource,[out,retval] SAFEARRAY(BYTE)*
abDestination);
};

I'd like to call it in C# with a simple

byte[] Dest = DoHash("A source");

However I get a "Specified array was not of the expected type" exception.  I
suspect I might need to hand-craft an interface definition (rather than use
the interop) but I don't know how.
"Peter Huang" [MSFT] - 06 Oct 2005 07:31 GMT
Hi

Based on my test, it seems to be works at my side based on the IDE
generated Interop Assembly.
VC.NET C#.NET 2003

[C++]
[
    object,
    uuid("FBBDA07C-B48D-4B00-A24F-BEFD7EA52118"),
    dual,    helpstring("ITestHash Interface"),
    pointer_default(unique)
]
__interface ITestHash : IDispatch
{
   
    [id(1), helpstring("method DoHash")] HRESULT DoHash([in] BSTR strSource,
[out,retval,satype(byte)] SAFEARRAY** abDestination);
};

STDMETHODIMP CTestHash::DoHash(BSTR strSource, SAFEARRAY** abDestination)
{
    CComBSTR bst = strSource;
    CComSafeArray<byte> m_bts;
    unsigned int l = bst.Length();
    byte bt = l;;
    m_bts.Add(bt);
    m_bts.Add(2);
    *abDestination = m_bts.Detach();
    return S_OK;
}

Add reference to the COM above will get a interop assembly for use in C#
[C#]
[STAThread]
static void Main(string[] args)
{
    COMLib.ITestHash t = new COMLib.CTestHashClass();
    byte[] bt = (byte[])t.DoHash("Hello");
    Console.WriteLine(bt[0]);
    Console.WriteLine(bt[1]);
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Graham Morris - 06 Oct 2005 15:39 GMT
You are using attributed C++: I had a C++ component with a separate IDL
file. The C++ component works fine with a VB6 client.

Anyway I've programmed round this problem now. (I've done what I wanted
another way).  Thanks for the reply.

> Hi
>
[quoted text clipped - 47 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 07 Oct 2005 03:12 GMT
Hi

Thanks for your quickly reply!
And I am glad that you have resolved it.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


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.