HighTechTalks DotNet Forums  

MethodInfo/Invoke/String

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss MethodInfo/Invoke/String in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Martin Madreza
 
Posts: n/a

Default MethodInfo/Invoke/String - 12-06-2007 , 04:31 AM






Hi,

I've a problem with Strings and MethodInfo Invoke. All Strings I give
to the Method I invoke where cut to on sign (like char).

The parameters are in a object

new object[]
{
"string1" ,
"string2", "string3"
"string4",
"string5",
11111,
"string6"
}

I testet with '\0' or StringBuilder, nothing works. The string is
always a char 's' with the invoke method.
Does anyone know why or can give me a link.

MethodInfo mi = moduleBuilder.GetMethod(function);
return mi.Invoke(null, args)

The DllImport function looks like that:
[DllImport("demo.dll", CharSet = CharSet.Ansi)]
public static extern int demoMethode(string filename, string
password,
string license_name, string license_key, int license_code,
string options);

I use the following Invoke Method:

public static object Invoke1(string dllName, string
function,object[] args, Type resultType)
{
Type[] argTypes = Type.GetTypeArray(args);

CultureInfo ci = Thread.CurrentThread.CurrentCulture;

AssemblyName asmName = new AssemblyName();
asmName.Name = "TempAssembly";
asmName.Version = new Version(1, 0, 0, 0);
asmName.CultureInfo = ci;

AssemblyBuilder asmBuilder =
Thread.GetDomain().DefineDynamicAssembly(asmName,

AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
asmBuilder.DefineDynamicModule("TempModule");

MethodBuilder methodBuilder =
moduleBuilder.DefineGlobalMethod(function,

MethodAttributes.Final | MethodAttributes.PinvokeImpl |

MethodAttributes.Public | MethodAttributes.Static,

CallingConventions.Standard,
resultType,
argTypes);

Type dllImportType = typeof (DllImportAttribute);
ConstructorInfo conInfo = dllImportType.GetConstructor(new
Type[] {typeof (string)});
FieldInfo callingConvField =
dllImportType.GetField("CallingConvention");
FieldInfo preserveSigField =
dllImportType.GetField("PreserveSig");
FieldInfo charSetField =
dllImportType.GetField("CharSet");
CustomAttributeBuilder attrBuilder = new
CustomAttributeBuilder(
conInfo,
new object[] {dllName},
new PropertyInfo[0],
new object[0],
new FieldInfo[]
{
callingConvField, preserveSigField,
charSetField
},
new object[] {CallingConvention.Winapi, true,
CharSet.Unicode}
);
methodBuilder.SetCustomAttribute(attrBuilder);
moduleBuilder.CreateGlobalFunctions();
MethodInfo mi = moduleBuilder.GetMethod(function);
return mi.Invoke(null, args);
}

Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: MethodInfo/Invoke/String - 12-06-2007 , 04:45 AM






Martin Madreza <madmakdread (AT) yahoo (DOT) de> wrote:
Quote:
I've a problem with Strings and MethodInfo Invoke. All Strings I give
to the Method I invoke where cut to on sign (like char).
Well, I didn't really understand the second sentence there, but your
sample code is doing lots of different things, including building a
dynamic assembly. Do you get the same issue when using a "normal"
assembly? Creating a short but complete app which does no extra work
would help a lot.

See http://pobox.com/~skeet/csharp/complete.html

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #3  
Old   
Willy Denoyette [MVP]
 
Posts: n/a

Default Re: MethodInfo/Invoke/String - 12-13-2007 , 05:56 PM



"Martin Madreza" <madmakdread (AT) yahoo (DOT) de> wrote

Quote:
Hi,

I've a problem with Strings and MethodInfo Invoke. All Strings I give
to the Method I invoke where cut to on sign (like char).

The parameters are in a object

new object[]
{
"string1" ,
"string2", "string3"
"string4",
"string5",
11111,
"string6"
}

I testet with '\0' or StringBuilder, nothing works. The string is
always a char 's' with the invoke method.
Does anyone know why or can give me a link.

MethodInfo mi = moduleBuilder.GetMethod(function);
return mi.Invoke(null, args)

The DllImport function looks like that:
[DllImport("demo.dll", CharSet = CharSet.Ansi)]
public static extern int demoMethode(string filename, string
password,
string license_name, string license_key, int license_code,
string options);

I use the following Invoke Method:

public static object Invoke1(string dllName, string
function,object[] args, Type resultType)
{
Type[] argTypes = Type.GetTypeArray(args);

CultureInfo ci = Thread.CurrentThread.CurrentCulture;

AssemblyName asmName = new AssemblyName();
asmName.Name = "TempAssembly";
asmName.Version = new Version(1, 0, 0, 0);
asmName.CultureInfo = ci;

AssemblyBuilder asmBuilder =
Thread.GetDomain().DefineDynamicAssembly(asmName,

AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
asmBuilder.DefineDynamicModule("TempModule");

MethodBuilder methodBuilder =
moduleBuilder.DefineGlobalMethod(function,

MethodAttributes.Final | MethodAttributes.PinvokeImpl |

MethodAttributes.Public | MethodAttributes.Static,

CallingConventions.Standard,
resultType,
argTypes);

Type dllImportType = typeof (DllImportAttribute);
ConstructorInfo conInfo = dllImportType.GetConstructor(new
Type[] {typeof (string)});
FieldInfo callingConvField =
dllImportType.GetField("CallingConvention");
FieldInfo preserveSigField =
dllImportType.GetField("PreserveSig");
FieldInfo charSetField =
dllImportType.GetField("CharSet");
CustomAttributeBuilder attrBuilder = new
CustomAttributeBuilder(
conInfo,
new object[] {dllName},
new PropertyInfo[0],
new object[0],
new FieldInfo[]
{
callingConvField, preserveSigField,
charSetField
},
new object[] {CallingConvention.Winapi, true,
CharSet.Unicode}
);
methodBuilder.SetCustomAttribute(attrBuilder);
moduleBuilder.CreateGlobalFunctions();
MethodInfo mi = moduleBuilder.GetMethod(function);
return mi.Invoke(null, args);
}


Are you aware that the DllImport expects "CharSet = CharSet.Ansi" while in
code you are creating the attribute as CharSet.Unicode?

Willy.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.