472,779 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 3616

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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

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.