Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2007

Tip: Looking for answers? Try searching our database.

MethodInfo/Invoke/String

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Madreza - 06 Dec 2007 09:31 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.