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);
}
Jon Skeet [C# MVP] - 06 Dec 2007 09:45 GMT
> 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

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Willy Denoyette [MVP] - 13 Dec 2007 22:56 GMT
> Hi,
>
[quoted text clipped - 87 lines]
> 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.