Type * can not be marshaled as an unmanaged structure

T

the openBack

I'm having a problem that I don't quite understand. I'm using someone
else's class:


using HANDLE = System.IntPtr;

namespace Win32 {
// other stuff here

public struct SHFILEINFO {
public HANDLE hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=Kernel.MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
public string szTypeName;
}

// other stuff here

}

but when I try to use it in a shell function I get an exception:
"Type Win32.SHFILEINFO can not be marshaled as an unmanaged structure;
no meaningful size or offset can be computed." when I attempt to use

System.Marshal.SizeOf(shinfo)

in a function call (as a parameter to SHGetFileInfo) where shinfo is an
SHFILEINFO object. I don't understand the marshalling very well, so this
is kind of stumping me. Can anyone help?
 
M

Mattias Sjögren

[MarshalAs(UnmanagedType.ByValArray, SizeConst=Kernel.MAX_PATH)]
public string szDisplayName;

Change ByValArray to ByValTStr. ByValArray can only be used with
arrays.



Mattias
 
D

David Browne

the openBack said:
I'm having a problem that I don't quite understand. I'm using someone
else's class:


using HANDLE = System.IntPtr;

namespace Win32 {
// other stuff here

public struct SHFILEINFO {
public HANDLE hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=Kernel.MAX_PATH)] public
string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
public string szTypeName;
}

// other stuff here

}

but when I try to use it in a shell function I get an exception:
"Type Win32.SHFILEINFO can not be marshaled as an unmanaged structure; no
meaningful size or offset can be computed." when I attempt to use

System.Marshal.SizeOf(shinfo)

Marshal.SizeOf() is not the same thing as C's sizeof operator. You need to
determine the size of the marshalled structure youself and pass it to the
function. Also you can always marshal it as a big byte array and sort it
out after the fact.

David
 
D

David Browne

Scratch that.

David
David Browne said:
the openBack said:
I'm having a problem that I don't quite understand. I'm using someone
else's class:


using HANDLE = System.IntPtr;

namespace Win32 {
// other stuff here

public struct SHFILEINFO {
public HANDLE hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=Kernel.MAX_PATH)] public
string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
public string szTypeName;
}

// other stuff here

}

but when I try to use it in a shell function I get an exception:
"Type Win32.SHFILEINFO can not be marshaled as an unmanaged structure; no
meaningful size or offset can be computed." when I attempt to use

System.Marshal.SizeOf(shinfo)

Marshal.SizeOf() is not the same thing as C's sizeof operator. You need
to determine the size of the marshalled structure youself and pass it to
the function. Also you can always marshal it as a big byte array and sort
it out after the fact.

David
 
T

the openBack

Mattias said:
[MarshalAs(UnmanagedType.ByValArray, SizeConst=Kernel.MAX_PATH)]
public string szDisplayName;


Change ByValArray to ByValTStr. ByValArray can only be used with
arrays.



Mattias

That was it, thank you! I should've seen something like that, but "Marshal" is very vague to me.
THANKS!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top