473,397 Members | 2,099 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,397 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 3682

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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.