473,695 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\Te sts>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.Argument Exception: Object type cannot be
converted t
o target type.
at System.Reflecti on.RuntimeMetho dInfo.InternalI nvoke(Object obj,
BindingFlag
s invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean i
sBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflecti on.RuntimeMetho dInfo.InternalI nvoke(Object obj,
BindingFlag
s invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean v
erifyAccess)
at System.Reflecti on.RuntimeMetho dInfo.Invoke(Ob ject obj,
BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at SBInvoke.test()
at SBInvoke.Main(S tring[] 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()

abcdefghijklmno pqrstuvwxyz

--file: SBInvoke.cs
using System;
using System.Runtime. InteropServices ;
using System.Reflecti on;

public class SBInvoke
{
public unsafe static void test()
{
SByte *memory = null;
#if DYNAMIC
Console.WriteLi ne("DYNAMIC invoke of GetMemory()");

Assembly assembly = Assembly.LoadFr om("sb.dll");
Type type = assembly.GetTyp e("SB");

/* find GetMemory() method */
MethodInfo method2invoke = null;
foreach (MethodInfo method in type.GetMethods ())
{
if (method.Name.Co mpareTo("GetMem ory") == 0)
method2invoke = method;
}

object[] parms = { (IntPtr)memory };

int size = (int)method2inv oke.Invoke(meth od2invoke,parms );

byte *countPtr = (byte*)(IntPtr) parms[0];
#else
Console.WriteLi ne("ILREF invoke of GetMemory()");

int size = SB.GetMemory(re f memory);
byte *countPtr = (byte*)memory;
#endif

Console.WriteLi ne();

for(int c=0; c<size; c++)
{
Console.Write(( char)*countPtr) ;
countPtr++;
}

Console.WriteLi ne();
}

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_ATTRIBU TE_OR_TWO_HELP
[MarshalAs(Unman agedType.LPArra y)]
#endif
ref SByte *ptr)
{
ptr = (SByte*)Marshal .AllocHGlobal(2 6);

byte *initPtr = (byte*)ptr;
for(int c=0; c<26; c++)
*initPtr++=(byt e)('a'+(byte)c) ;

return 26;
}
}

using System.Runtime. InteropServices ;
using System;

public class SB
{
public unsafe static int GetMemory(
#if DOES_AN_ATTRIBU TE_OR_TWO_HELP
[MarshalAs(Unman agedType.LPArra y)]
#endif
ref SByte *ptr)
{
ptr = (SByte*)Marshal .AllocHGlobal(2 6);

byte *initPtr = (byte*)ptr;
for(int c=0; c<26; c++)
*initPtr++=(byt e)('a'+(byte)c) ;

return 26;
}
}
using System;
using System.Runtime. InteropServices ;
using System.Reflecti on;

public class SBInvoke
{
public unsafe static void test()
{
SByte *memory = null;
#if DYNAMIC
Console.WriteLi ne("DYNAMIC invoke of GetMemory()");

Assembly assembly = Assembly.LoadFr om("sb.dll");
Type type = assembly.GetTyp e("SB");

/* find GetMemory() method */
MethodInfo method2invoke = null;
foreach (MethodInfo method in type.GetMethods ())
{
if (method.Name.Co mpareTo("GetMem ory") == 0)
method2invoke = method;
}

object[] parms = { (IntPtr)memory };

int size = (int)method2inv oke.Invoke(meth od2invoke,parms );

byte *countPtr = (byte*)(IntPtr) parms[0];
#else
Console.WriteLi ne("ILREF invoke of GetMemory()");

int size = SB.GetMemory(re f memory);
byte *countPtr = (byte*)memory;
#endif

Console.WriteLi ne();

for(int c=0; c<size; c++)
{
Console.Write(( char)*countPtr) ;
countPtr++;
}

Console.WriteLi ne();
}

public static int Main(string[] args)
{
test();
return 0;
}
}
Nov 15 '05 #1
0 3703

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

Similar topics

22
3330
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
1312
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 below... --
3
4567
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 stopped me in my tracks. I must have used FileStream.Read a thousand times and it never dawned on me that I never have to specify either an "out" or "ref" when I pass the byte array that I pass to this function- yet it returns the bytes read from the...
3
1740
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 queries a database and // formats the return DataSet as HTML and inserts // this HTML into strHTML for return to the calling
5
6696
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
2675
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 in a DLL project:
0
8623
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
1
8839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7656
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6488
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5837
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2997
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2269
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.