S
Stephen Gennard
Hello,
I having a problem dynamically invoking a static method that takes a
reference to a SByte*. If I do it directly it works just fine.
Anyone any ideas why?
I have include a example below...
--
Stephen Gennard
email: (e-mail address removed)
D:\development>csc /unsafe /target:library /out:sb.dll sb.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
D:\development\netx45\ilrts\Tests>csc /unsafe /defineYNAMIC sbinvoke.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
D:\development>sbinvoke
DYNAMIC invoke of GetMemory()
Unhandled Exception: System.ArgumentException: Object type cannot be
converted t
o target type.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlag
s invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean i
sBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlag
s invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean v
erifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at SBInvoke.test()
at SBInvoke.Main(String[] args)
Now for the one that works...
D:\development> csc /unsafe sbinvoke.cs /r:sb.dll
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
D:\development>sbinvoke
ILREF invoke of GetMemory()
abcdefghijklmnopqrstuvwxyz
--file: SBInvoke.cs
using System;
using System.Runtime.InteropServices;
using System.Reflection;
public class SBInvoke
{
public unsafe static void test()
{
SByte *memory = null;
#if DYNAMIC
Console.WriteLine("DYNAMIC invoke of GetMemory()");
Assembly assembly = Assembly.LoadFrom("sb.dll");
Type type = assembly.GetType("SB");
/* find GetMemory() method */
MethodInfo method2invoke = null;
foreach (MethodInfo method in type.GetMethods())
{
if (method.Name.CompareTo("GetMemory") == 0)
method2invoke = method;
}
object[] parms = { (IntPtr)memory };
int size = (int)method2invoke.Invoke(method2invoke,parms);
byte *countPtr = (byte*)(IntPtr)parms[0];
#else
Console.WriteLine("ILREF invoke of GetMemory()");
int size = SB.GetMemory(ref memory);
byte *countPtr = (byte*)memory;
#endif
Console.WriteLine();
for(int c=0; c<size; c++)
{
Console.Write((char)*countPtr);
countPtr++;
}
Console.WriteLine();
}
public static int Main(string[] args)
{
test();
return 0;
}
}
--file: sb.cs
using System.Runtime.InteropServices;
using System;
public class SB
{
public unsafe static int GetMemory(
#if DOES_AN_ATTRIBUTE_OR_TWO_HELP
[MarshalAs(UnmanagedType.LPArray)]
#endif
ref SByte *ptr)
{
ptr = (SByte*)Marshal.AllocHGlobal(26);
byte *initPtr = (byte*)ptr;
for(int c=0; c<26; c++)
*initPtr++=(byte)('a'+(byte)c);
return 26;
}
}
using System.Runtime.InteropServices;
using System;
public class SB
{
public unsafe static int GetMemory(
#if DOES_AN_ATTRIBUTE_OR_TWO_HELP
[MarshalAs(UnmanagedType.LPArray)]
#endif
ref SByte *ptr)
{
ptr = (SByte*)Marshal.AllocHGlobal(26);
byte *initPtr = (byte*)ptr;
for(int c=0; c<26; c++)
*initPtr++=(byte)('a'+(byte)c);
return 26;
}
}
using System;
using System.Runtime.InteropServices;
using System.Reflection;
public class SBInvoke
{
public unsafe static void test()
{
SByte *memory = null;
#if DYNAMIC
Console.WriteLine("DYNAMIC invoke of GetMemory()");
Assembly assembly = Assembly.LoadFrom("sb.dll");
Type type = assembly.GetType("SB");
/* find GetMemory() method */
MethodInfo method2invoke = null;
foreach (MethodInfo method in type.GetMethods())
{
if (method.Name.CompareTo("GetMemory") == 0)
method2invoke = method;
}
object[] parms = { (IntPtr)memory };
int size = (int)method2invoke.Invoke(method2invoke,parms);
byte *countPtr = (byte*)(IntPtr)parms[0];
#else
Console.WriteLine("ILREF invoke of GetMemory()");
int size = SB.GetMemory(ref memory);
byte *countPtr = (byte*)memory;
#endif
Console.WriteLine();
for(int c=0; c<size; c++)
{
Console.Write((char)*countPtr);
countPtr++;
}
Console.WriteLine();
}
public static int Main(string[] args)
{
test();
return 0;
}
}
I having a problem dynamically invoking a static method that takes a
reference to a SByte*. If I do it directly it works just fine.
Anyone any ideas why?
I have include a example below...
--
Stephen Gennard
email: (e-mail address removed)
D:\development>csc /unsafe /target:library /out:sb.dll sb.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
D:\development\netx45\ilrts\Tests>csc /unsafe /defineYNAMIC sbinvoke.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
D:\development>sbinvoke
DYNAMIC invoke of GetMemory()
Unhandled Exception: System.ArgumentException: Object type cannot be
converted t
o target type.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlag
s invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean i
sBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlag
s invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean v
erifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at SBInvoke.test()
at SBInvoke.Main(String[] args)
Now for the one that works...
D:\development> csc /unsafe sbinvoke.cs /r:sb.dll
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
D:\development>sbinvoke
ILREF invoke of GetMemory()
abcdefghijklmnopqrstuvwxyz
--file: SBInvoke.cs
using System;
using System.Runtime.InteropServices;
using System.Reflection;
public class SBInvoke
{
public unsafe static void test()
{
SByte *memory = null;
#if DYNAMIC
Console.WriteLine("DYNAMIC invoke of GetMemory()");
Assembly assembly = Assembly.LoadFrom("sb.dll");
Type type = assembly.GetType("SB");
/* find GetMemory() method */
MethodInfo method2invoke = null;
foreach (MethodInfo method in type.GetMethods())
{
if (method.Name.CompareTo("GetMemory") == 0)
method2invoke = method;
}
object[] parms = { (IntPtr)memory };
int size = (int)method2invoke.Invoke(method2invoke,parms);
byte *countPtr = (byte*)(IntPtr)parms[0];
#else
Console.WriteLine("ILREF invoke of GetMemory()");
int size = SB.GetMemory(ref memory);
byte *countPtr = (byte*)memory;
#endif
Console.WriteLine();
for(int c=0; c<size; c++)
{
Console.Write((char)*countPtr);
countPtr++;
}
Console.WriteLine();
}
public static int Main(string[] args)
{
test();
return 0;
}
}
--file: sb.cs
using System.Runtime.InteropServices;
using System;
public class SB
{
public unsafe static int GetMemory(
#if DOES_AN_ATTRIBUTE_OR_TWO_HELP
[MarshalAs(UnmanagedType.LPArray)]
#endif
ref SByte *ptr)
{
ptr = (SByte*)Marshal.AllocHGlobal(26);
byte *initPtr = (byte*)ptr;
for(int c=0; c<26; c++)
*initPtr++=(byte)('a'+(byte)c);
return 26;
}
}
using System.Runtime.InteropServices;
using System;
public class SB
{
public unsafe static int GetMemory(
#if DOES_AN_ATTRIBUTE_OR_TWO_HELP
[MarshalAs(UnmanagedType.LPArray)]
#endif
ref SByte *ptr)
{
ptr = (SByte*)Marshal.AllocHGlobal(26);
byte *initPtr = (byte*)ptr;
for(int c=0; c<26; c++)
*initPtr++=(byte)('a'+(byte)c);
return 26;
}
}
using System;
using System.Runtime.InteropServices;
using System.Reflection;
public class SBInvoke
{
public unsafe static void test()
{
SByte *memory = null;
#if DYNAMIC
Console.WriteLine("DYNAMIC invoke of GetMemory()");
Assembly assembly = Assembly.LoadFrom("sb.dll");
Type type = assembly.GetType("SB");
/* find GetMemory() method */
MethodInfo method2invoke = null;
foreach (MethodInfo method in type.GetMethods())
{
if (method.Name.CompareTo("GetMemory") == 0)
method2invoke = method;
}
object[] parms = { (IntPtr)memory };
int size = (int)method2invoke.Invoke(method2invoke,parms);
byte *countPtr = (byte*)(IntPtr)parms[0];
#else
Console.WriteLine("ILREF invoke of GetMemory()");
int size = SB.GetMemory(ref memory);
byte *countPtr = (byte*)memory;
#endif
Console.WriteLine();
for(int c=0; c<size; c++)
{
Console.Write((char)*countPtr);
countPtr++;
}
Console.WriteLine();
}
public static int Main(string[] args)
{
test();
return 0;
}
}