Reflection and PInvoke?

A

Abhi

I am using Reflection (.Net 3.0) to invoke certain methods in C# dll. There
are certain methods in C# dll which uses PInvoke to call method in native dll
for example:
[DllImport(NativeMethodDLL, CharSet = CharSet.Auto)]
public static extern IntPtr NativeMethod();

Public void SampleMethods()
{
NativeMethod();
}

and I get a TargetInvocationException exception when I call Invoke method of
MethodInfo class to invoke SampleMethods()

Question: Is is possible to call method using reflection which indirectly
calls native method?

Thanks,
Abhi
 
J

Jeroen Mostert

Abhi said:
I am using Reflection (.Net 3.0) to invoke certain methods in C# dll. There
are certain methods in C# dll which uses PInvoke to call method in native dll
for example:
[DllImport(NativeMethodDLL, CharSet = CharSet.Auto)]
public static extern IntPtr NativeMethod();

Public void SampleMethods()
{
NativeMethod();
}

and I get a TargetInvocationException exception when I call Invoke method of
MethodInfo class to invoke SampleMethods()

Question: Is is possible to call method using reflection which indirectly
calls native method?
It certainly should be. Have you checked the InnerException of the
TargetInvocationException? Chances are the reflection is going fine and the
method itself is just throwing an exception.
 
A

Abhi

Thanks Jeroen!!..I looked into the inner exception and method invoke was
failing because native dll location was incorrect in DllImport. Bottomline is
we can very well call native method via reflection

Jeroen Mostert said:
Abhi said:
I am using Reflection (.Net 3.0) to invoke certain methods in C# dll. There
are certain methods in C# dll which uses PInvoke to call method in native dll
for example:
[DllImport(NativeMethodDLL, CharSet = CharSet.Auto)]
public static extern IntPtr NativeMethod();

Public void SampleMethods()
{
NativeMethod();
}

and I get a TargetInvocationException exception when I call Invoke method of
MethodInfo class to invoke SampleMethods()

Question: Is is possible to call method using reflection which indirectly
calls native method?
It certainly should be. Have you checked the InnerException of the
TargetInvocationException? Chances are the reflection is going fine and the
method itself is just throwing an exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top