PSReflect icon indicating copy to clipboard operation
PSReflect copied to clipboard

Unable to Marshal function parameters

Open jaredcatkinson opened this issue 7 years ago • 4 comments

PSReflect does not have the ability to use the MarshalAs attribute on function parameters.

Below is an example P/Invoke definition where the MarshalAs attribute is used on the pgActionID parameter:

[DllImport("wintrust.dll", ExactSpelling = true, SetLastError = false, CharSet = CharSet.Unicode)]
static extern WinVerifyTrustResult WinVerifyTrust(
	[In] IntPtr hwnd,
	[In] [MarshalAs(UnmanagedType.LPStruct)] Guid pgActionID, 
	[In] WinTrustData pWVTData
);

jaredcatkinson avatar Jun 22 '17 02:06 jaredcatkinson

Should be doable using the ParameterBuilder.SetCustomAttribute method in a similar fashion to how I do it with struct members. Before I dive into this, could you offer some additional use cases where you might need this? In the example above, you can get away without the MarshalAs attribute by specifying pgActionID as [Guid].MakeByRefType().

mattifestation avatar Jun 25 '17 18:06 mattifestation

I have a similar issue where I can't marshal return values

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

jambonmcyeah avatar Jul 05 '17 00:07 jambonmcyeah

For this example can't you specify the return type as [bool]? I think the marshaling would happen automatically.

Something like: (func user32 EnumChildWindows ([bool]) @( [IntPtr], [IntPtr], [IntPtr] ))

Sent from my iPhone

On Jul 4, 2017, at 5:32 PM, jambonmcyeah [email protected] wrote:

I have a similar issue where I can't marshal return values

[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam); — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jaredcatkinson avatar Jul 05 '17 00:07 jaredcatkinson

Well, what there is a function where marshaling won't happen automatically? I remembered a while ago I neede a function with a UnmanagedType.LPArray return type. Forgot which function though.

jambonmcyeah avatar Jul 05 '17 01:07 jambonmcyeah