cffm Posted February 10, 2014 at 07:50 PM Report #544732 Posted February 10, 2014 at 07:50 PM Boa noite, tenho uma dll qye foi desenvolvida em c e estou a tentar utilizá-la em c#. No entanto dá-me o seguinte erro: "Tentativa de ler ou escrever na memória protegida. Isto é normalmente uma indicação de que existe outra memória danificada." [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] public static extern IntPtr LoadLibrary(string dllToLoad); [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); [DllImport("kernel32")] public static extern bool FreeLibrary(IntPtr hModule); [DllImport("lib.dll", EntryPoint = "LoadRes", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern SRes LoadRes([MarshalAs(UnmanagedType.LPStr)]string str, bool type); SRes sres = new SRes(); string path="C:\\123456.fil"; System.IntPtr load = LoadLibrary("lib.dll"); System.IntPtr adress = GetProcAddress(load, "LoadRes"); sres = LoadRes(path, true); E esta é a linha da dll em c. __declspec(dllexport) SRes LoadRes( const char *filename, bool header_only ); Muito obrigado pela ajuda!
Gnrtuga Posted February 11, 2014 at 07:43 AM Report #544786 Posted February 11, 2014 at 07:43 AM Boas, eu não pesco nada disso, mas olha aqui e vê se fizeste tudo bem: http://nawattlabs.com/index.php?option=com_content&view=article&id=20:using-c-dlls-c&catid=9:blog&Itemid=5 e http://www.nag.co.uk/IndustryArticles/Calling_C_Library_DLLs_from_C_sharp.pdf Boa sorte. Férias! Estou por aqui: http://maps.google.p...001549&t=h&z=20 (a bulir claro está!) Nunca mais é verão outra vez.. :)
cffm Posted February 11, 2014 at 01:49 PM Author Report #544837 Posted February 11, 2014 at 01:49 PM Obrigado, mas a DLL está a funcionar com código em C, em C# é que não.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now