01-14-2010 09:59 AM
Hi,
I need to have the MetadataToken of a method in a DLL .NET. For information, you can see why i need this here : http://forums.ni.com/ni/board/message?board.id=330&thread.id=26880
I have a method in .NET who can do this, but I would like to transform this .NET method to Labwindows CVI :
public int GetMethodMetadataToken(string assemPath, string fullClassName, string methodName)
{
MethodInfo myMthdInfo;
int metadataToken = -1;
Assembly myAssem = Assembly.LoadFrom(assemPath);
Type myAssemType = myAssem.GetType("myTestAssembly.Class1");
myMthdInfo = myAssemType.GetMethod(methodName);
metadataToken = myMthdInfo.MetadataToken;
return metadataToken;
}
So i get this in Labwindws CVI in including "mscorlib2.h":
long MetadataToken = 0;
System_Reflection_Assembly ReflectionAssembly;
CDotNetHandle ExceptionHandle;
System_Type Type;
System_Reflection_MethodInfo ReflectionMethodInfo;
Initialize_mscorlib ();
System_Reflection_Assembly_LoadFrom(sModulePath, &ReflectionAssembly, &ExceptionHandle);
System_Reflection_Assembly_GetType_1(ReflectionAssembly,sClassName, &Type, &ExceptionHandle);
System_Type_GetMethod_5 (Type,sFunctionName,&ReflectionMethodInfo,&ExceptionHandle);
System_Reflection_MethodInfo_Get_MetadataToken(ReflectionMethodInfo,&MetadataToken, &ExceptionHandle);
Close_mscorlib ();
But i get a FATAL RUN-TIME ERROR: The program has caused a 'General Protection' fault at 0023:004EFE37 on the function System_Reflection_MethodInfo_GetMetadataToken.
Have you got an idea what i do wrong?
Anthony
Solved! Go to Solution.
01-15-2010 11:34 AM
Hey Anthony -
I tried the code you posted, and didn't see the same crash you are seeing. It returned to me the same metadata token that I received from similar code in C#. Here's the code I used to perform your test:
Does this code run for you?
NickB
National Instruments
01-18-2010 08:42 AM
Hi NickB,
I couldn't run your code because it didn't find the System.Drawing.dll and I got "The target invoked by the LabWindows/CVI .NET Library threw an exception."
So I changed assmName by : static char * assmName = "C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\System.Drawing.dll";
And when i changed this i got the same FATAL RUN-TIME ERROR on errChk (System_Reflection_MethodInfo_Get_MetadataToken (methodInfo, &token, &hException));
For information, I used mscorlib2.fp in the folder C:\Users\Public\Documents\National Instruments\CVI90\samples\dotnet
Anthony
01-18-2010 09:45 AM
Hey Anthony -
I forgot to mention I had copied System.Drawing.dll locally to my application directory for testing purposes. Without it there, you would have to specify a full path. My mistake.
The first thing I see is that you're trying to load System.Drawing.dll from the framework64 location. This location is home to both platform agnostic and 64 bit targeting assemblies. The reason you are able to load System.Drawing.dll from this location is that it is platform agnostic, but because CVI can only load and work with platform agnostic assemblies and 32 bit targeting assemblies, as a general rule, you should try to load from the framework location.
However, I don't think this is the root of your issue, because I'm still able to run my example code with the path changed as you had indicated. One thing you could try to troubleshoot the issue is to add mscorlib2.c to your project. The crash address you're reporting looks like it's coming from your module, and so my guess is that it's crashing in the mscorlib2.c source. If you could give this a try and let me know what you find, I'll take another look with you.
NickB
National Instruments
01-19-2010 04:35 AM
Hey NickB,
Thank you for your answer.
I've added mscorlib2.c to my project. And now it works well!
Anthony