More PInvoke Issues
this works -
[dllimport("module.dll")]
public static extern intptr createmodule();
intptr modulepointer = createmodule();
logwindow.instance.addoutput( "pointer = " + modulepointer );
and prints out - 105987728
this doesn't work -
modulebuilder modulebuilder;
assemblybuilder assemblybuilder;
assemblyname assemblyname = new assemblyname();
assemblyname.name = modulename;
assemblybuilder = appdomain.currentdomain.definedynamicassembly( assemblyname, assemblybuilderaccess.run );
modulebuilder = assemblybuilder.definedynamicmodule( modulename );
// createmodule function
methodbuilder mb;
mb = modulebuilder.definepinvokemethod( "createmodule", dllfile, methodattributes.public | methodattributes.static | methodattributes.pinvokeimpl,
callingconventions.standard, typeof( intptr ), null, callingconvention.cdecl, charset.unicode );
modulebuilder.createglobalfunctions();
module module = new module();
module.m_createmodulefunction = modulebuilder.getmethod( "createmodule" );
module.m_modulepointer = (intptr)module.m_createmodulefunction.invoke( null, null );
logwindow.instance.addoutput( "pointer = " + module.m_modulepointer );
everything works, call made successfully, however, returns 0 pointer.
i need 2nd code block( dynamic loading of dlls work ). don't understand why it's returning 0. in c++ dll, write out pointer being returned file, , it's valid. have clue i'm doing wrong?
i've reproduced issue , reported our connect portal, can check out: http://connect.microsoft.com/visualstudio/feedback/viewfeedback.aspx?feedbackid=319570
thank valuable feedback. our developers investigating , hope can satisfying answer.
as work around, can add line code make works:
mb.setimplementationflags(methodimplattributes.preservesig);
preservesigattribute indicates hresult or retval signature transformation takes place during com interop calls should suppressed. can check out the methodimplattributes enumeration (system.reflection).
.NET Framework > Common Language Runtime Internals and Architecture
Comments
Post a Comment