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 2007

Tip: Looking for answers? Try searching our database.

Cannot marshal 'return value'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ShaunO - 14 May 2007 08:26 GMT
Hi,
I am trying to call an old Delphi .dll

// DELPHI definition (should return an array of 256 chars)
function DiskSaveFooter(missionname,qlffilename,id:pchar):TCharArray; export;

// My C# definition
[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]

public static extern char[] DiskSaveFooter(string missionname, string
qlffilename, string id);

// My C# Implementation
char []b = new char[256];
b = qn.DiskSaveFooter("mis1", "", "000:000");

I get the ERROR: Cannot marshal 'return value': Invalid managed/unmanaged
type combination.

Any suggestions please?
G Himangi - 16 May 2007 06:31 GMT
It may work if you define the function as

[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]

public static extern IntPtr DiskSaveFooter(string missionname, string
qlffilename, string id);

and then.....

IntPtr ptr = qn.DiskSaveFooter("mis1", "", "000:000");
char* charArray = (char*)ptr;

this requires unsafe code though.

Or you can use Marshal.Copy to copy the contents of the ptr to a char/byte
array.

---------
- G Himangi,   Sky Software       http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
---------

> Hi,
> I am trying to call an old Delphi .dll
[quoted text clipped - 18 lines]
>
> Any suggestions please?
ShaunO - 16 May 2007 15:12 GMT
Thanks for the suggestion.
It errors with
A call to PInvoke function 'DLLTest!DLLTest.Q::DiskSaveFooter' has
unbalanced the stack. This is likely because the managed PInvoke signature
does not match the unmanaged target signature. Check that the calling
convention and parameters of the PInvoke signature match the target unmanaged
signature.

I get this error message on the
IntPtr ptr = qn.DiskSaveFooter("mis1", "", "000:000"); line.

I think that  Delphi's native strings are single-byte strings, whereas
.NET's are UTF-16 which may be affecting things but i get the above error
message when i define the return type as anything other than char[]

Does this help?

> It may work if you define the function as
>
[quoted text clipped - 45 lines]
> >
> > Any suggestions please?
G Himangi - 21 May 2007 12:53 GMT
You can try applying a MArshalAs(ByValArray, SIzeConst=260) to the return
type of your method signature and change the return type from char[] to
byte[]

Did that work?

---------
- G Himangi,   Sky Software       http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
---------

> Thanks for the suggestion.
> It errors with
[quoted text clipped - 66 lines]
>> >
>> > Any suggestions please?
ShaunO - 21 May 2007 20:09 GMT
Thanks but sadly not !
The closest implementation i can do is:

[return: MarshalAs(UnmanagedType.ByValArray, SizeConst = 260)]
       public static extern byte[] DiskSaveFooter(string missionname,
string qlffilename, string id);

but this gives a compile error of:
Error emitting 'System.Runtime.InteropServices.MarshalAsAttribute' attribute
-- 'Specified unmanaged type is only valid on fields.'

as a stab in the dark i managed to get this to compile
[return: MarshalAs(UnmanagedType.LPArray, SizeConst = 255)]
       public static extern Byte[] DiskSaveFooter(string missionname,
string qlffilename, string id);

but the following line of implementation (pretty basic)
DiskSaveFooter("mis1", "", "000:000");
gave the following error:
Cannot marshal 'return value': Invalid managed/unmanaged type combination.

Soooooo Stuck.
I cant believe something so simple can be so hard!
Shaun

> You can try applying a MArshalAs(ByValArray, SIzeConst=260) to the return
> type of your method signature and change the return type from char[] to
[quoted text clipped - 81 lines]
> >> >
> >> > Any suggestions please?

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.