HighTechTalks DotNet Forums  

Cannot marshal 'return value'

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss Cannot marshal 'return value' in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
ShaunO
 
Posts: n/a

Default Cannot marshal 'return value' - 05-14-2007 , 03:26 AM






Hi,
I am trying to call an old Delphi .dll

// DELPHI definition (should return an array of 256 chars)
function DiskSaveFooter(missionname,qlffilename,idchar):T CharArray; export;

// My C# definition
[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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?

Reply With Quote
  #2  
Old   
G Himangi
 
Posts: n/a

Default Re: Cannot marshal 'return value' - 05-16-2007 , 01:31 AM






It may work if you define the function as

[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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
---------



"ShaunO" <ShaunO (AT) discussions (DOT) microsoft.com> wrote

Quote:
Hi,
I am trying to call an old Delphi .dll

// DELPHI definition (should return an array of 256 chars)
function DiskSaveFooter(missionname,qlffilename,idchar):T CharArray;
export;

// My C# definition
[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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?



Reply With Quote
  #3  
Old   
ShaunO
 
Posts: n/a

Default Re: Cannot marshal 'return value' - 05-16-2007 , 10:12 AM



Thanks for the suggestion.
It errors with
A call to PInvoke function 'DLLTest!DLLTest.Q:iskSaveFooter' 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?



"G Himangi" wrote:

Quote:
It may work if you define the function as

[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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
---------



"ShaunO" <ShaunO (AT) discussions (DOT) microsoft.com> wrote in message
news:941ABD0B-C05C-4636-A2D9-8F247D252E54 (AT) microsoft (DOT) com...
Hi,
I am trying to call an old Delphi .dll

// DELPHI definition (should return an array of 256 chars)
function DiskSaveFooter(missionname,qlffilename,idchar):T CharArray;
export;

// My C# definition
[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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?




Reply With Quote
  #4  
Old   
G Himangi
 
Posts: n/a

Default Re: Cannot marshal 'return value' - 05-21-2007 , 07:53 AM



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
---------



"ShaunO" <ShaunO (AT) discussions (DOT) microsoft.com> wrote

Quote:
Thanks for the suggestion.
It errors with
A call to PInvoke function 'DLLTest!DLLTest.Q:iskSaveFooter' 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?



"G Himangi" wrote:

It may work if you define the function as

[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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
---------



"ShaunO" <ShaunO (AT) discussions (DOT) microsoft.com> wrote in message
news:941ABD0B-C05C-4636-A2D9-8F247D252E54 (AT) microsoft (DOT) com...
Hi,
I am trying to call an old Delphi .dll

// DELPHI definition (should return an array of 256 chars)
function DiskSaveFooter(missionname,qlffilename,idchar):T CharArray;
export;

// My C# definition
[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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?






Reply With Quote
  #5  
Old   
ShaunO
 
Posts: n/a

Default Re: Cannot marshal 'return value' - 05-21-2007 , 03:09 PM



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

"G Himangi" wrote:

Quote:
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
---------



"ShaunO" <ShaunO (AT) discussions (DOT) microsoft.com> wrote in message
news:7C3D477B-9C16-4011-8373-E88C572F9E7D (AT) microsoft (DOT) com...
Thanks for the suggestion.
It errors with
A call to PInvoke function 'DLLTest!DLLTest.Q:iskSaveFooter' 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?



"G Himangi" wrote:

It may work if you define the function as

[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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
---------



"ShaunO" <ShaunO (AT) discussions (DOT) microsoft.com> wrote in message
news:941ABD0B-C05C-4636-A2D9-8F247D252E54 (AT) microsoft (DOT) com...
Hi,
I am trying to call an old Delphi .dll

// DELPHI definition (should return an array of 256 chars)
function DiskSaveFooter(missionname,qlffilename,idchar):T CharArray;
export;

// My C# definition
[DllImport("qn.dll",
CharSet=CharSet.Ansi,CallingConvention=CallingConv ention.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?







Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.