473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dynamic Invokes and Marshaling a "ref SByte*"

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: St*************@microfocus.com

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 /define:DYNAMIC 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;
}
}
Nov 15 '05 #1
0 3678

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
3
by: Stephen Gennard | last post by:
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...
3
by: Gene | last post by:
I'm not sure I understand this behavior (at least in the context of C#). I understand the use of "ref" and "out" keywords as they apply to method arguments, and then something this morning...
3
by: Bob Maggio | last post by:
I have created a function that returns a string containing raw HTML to be used on a web form (see below): private string GetReportHTML() { string strHTML = ""; // I have some code here that...
5
by: Tee | last post by:
Hi guys, I am confused about the usage of "ref". Please see this code: private void button3_Click(object sender, System.EventArgs e) {
5
by: Cmtk Software | last post by:
The following code: public __gc class MyClass { public: void MyFunc (int __gc* number); }; Generates the following metadata for C# when compiled in VC 2005 with the /clr:oldsyntax switch...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.