473,898 Members | 3,028 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 3722

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

Similar topics

22
3366
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
4578
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
1758
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
6713
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
2688
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
9839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10853
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10480
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9658
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...
0
5876
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4701
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
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.