Answered by:
How Can load a non-completely managed c++ assembly in runtime

Question
-
Hello
i wrote an assembly(not managed completely ("1.dll"))
i want to load it at runtime (in a managed assembly) but when i try to load it it give me this error:
"Could not load file or assembly '1, Version=1.0.4207.24855, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)"
its the code that i use to load it from memory:
Dim D As Assembly = Assembly.Load(My.Resources.1)how i can load this file at runtime
Saturday, July 9, 2011 11:13 AM
Answers
-
Hi Hosein,
This is an known issue. So I suggest you the following workaround:
1. write the resource file into a temp file.
2. use the temp file.
3. delete the temp file.
Please try it, I hope this will be helpful.
Best regards,
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Hosein1373 Thursday, July 14, 2011 3:26 PM
Thursday, July 14, 2011 10:48 AM
All replies
-
You cannot load an unmanaged dll using Assembly.Load. You must use DllImport, as described here: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx.To locate the dll at runtime, standard locations such as system32 are searched, but you should place your dll into the folder where the executable resides for it to be correctly found.
--
MikeSaturday, July 9, 2011 12:10 PM -
This is a managed dll but not completely(you can add this to your project form "References / Add References") but there are unmanaged codes in this dll
Saturday, July 9, 2011 12:33 PM -
Hi Hosein1373,
Please note: the COM component can be add by "References / Add References", so this cannot prove the dll is a managed dll.
When you add the reference by "References / Add References", a easy way to check whether it is a managed dll is by checking the file type.
If it is ActiveX, it com type dll, on the other hand, it is managed.
See this picture to check the file type:
You can also try the following statement:
Dim D As Assembly = Assembly.LoadFrom(fileName)
Dim D As Assembly = Assembly.LoadFile(fileName)
Dim D As Assembly = Assembly.UnsafeLoadFrom(fileName)I hope it will be helpful, if you have any concerns, please feel free to let me know.
Best regards,
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Cor Ligthert Tuesday, July 12, 2011 3:37 PM
Tuesday, July 12, 2011 2:33 PM -
Thank you
i checked that and the File Type = Assembly
i should load this in runtime from memory
Tuesday, July 12, 2011 6:15 PM -
Hi Hosein1373,
How about the following method?
Dim D As Assembly = Assembly.LoadFrom(fileName)
Dim D As Assembly = Assembly.LoadFile(fileName)
Please note, the filename is just the file name, it is not the resource name. So please try it, if it still doesn't work, Can you please upload the DLL to the skydrive and post the download link here ? So the community members can check it and find the cause easily.Best regards,
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Wednesday, July 13, 2011 5:49 AM -
Hi Mike Feng,
these methods work correctly and load assembly from hdd but i need load this dll from memory (because assembly is always available and security reasons)
can i load this file from my resources or from memory
if you need this dll i can upload it
thanks
Wednesday, July 13, 2011 5:38 PM -
Hi Hosein,
This is an known issue. So I suggest you the following workaround:
1. write the resource file into a temp file.
2. use the temp file.
3. delete the temp file.
Please try it, I hope this will be helpful.
Best regards,
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Hosein1373 Thursday, July 14, 2011 3:26 PM
Thursday, July 14, 2011 10:48 AM -
Thank you
Thursday, July 14, 2011 3:58 PM -
Hi Mike Feng,
these methods work correctly and load assembly from hdd but i need load this dll from memory (because assembly is always available and security reasons)
can i load this file from my resources or from memory
if you need this dll i can upload it
thanks
I know you already accepted the answer from Mike but for future use, you can load the dll file from memory as byte array which you can pass to Assembly.Load method. Then any method can be invoked from the dll by passing the name with its parameter or argument if necessary. Below link have sample code, all you have to do is change Assembly.LoadFrom to Assembly.Load in order to read it from memory.
Dynamically load a class and execute a method in .net
kaymaf
CODE CONVERTER SITE
Thursday, July 14, 2011 9:41 PM -
thank you i tried that first(at first post i wrote this and the error)Friday, July 15, 2011 4:50 PM
-
Mike and Hosein1373,
I am having exactly the same problem and yes, Mike's solution (File.WriteAllBytes (); Assembly.LoadFrom (file)) does work but I can't delete the temp file once the assembly is loaded. I guess I could try and load it into a separate AppDomain then unload the AppDomain just before my program exits and delete the file... but
Can someone tell me, why loading from file works and loading the same assembly from memory doesn't? Why would the CLR care about where the assembly is loaded from? Any insights here Mike?
Monday, November 28, 2011 11:22 PM