dllimport

free memory not clears the memory block

余生长醉 提交于 2019-12-01 12:33:44
I am using DllImport to call method in c wrapper library from my own .net class. This method in c dll creates a string variable and returns the pointer of the string. Something like this; _declspec(dllexport) int ReturnString() { char* retval = (char *) malloc(125); strcat(retval, "SOMETEXT"); strcat(retval, "SOMETEXT MORE"); return (int)retval; } Then i read the string using Marshall.PtrToStringAnsi(ptr). After i get a copy of the string, i simply call another c method HeapDestroy which is in c wrapper library that calls free(ptr). Here is the question; Recently while it is working like a

C++ template specialization in different dll produces linker errors

▼魔方 西西 提交于 2019-12-01 12:19:11
问题 I have a third party dll that contains a template class with several specializations. I have my own specialization on Linux, trying to compile a windows dll however results in linker errors. I tried around a bit and found out that the dllimport specification on the template header may be the cause and removing it would fix my problem. However I don't want to modify or copy the header since it could break with every update to the third party library. Here is a minimal example to reproduce my

Using SetFilePointer in C# has unblanced the stack

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 11:10:22
问题 Ok, I am using the SetFilePointer function in C# with .NET 4.0. Below are the dllimports I used to call this function: [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] static extern uint SetFilePointer([In] SafeFileHandle hFile, [In] long lDistanceToMove, [Out] out int lpDistanceToMoveHigh, [In] EMoveMethod dwMoveMethod); Whenever I run the code that uses the SetFilePointer function in the debugger I get this exception:

free memory not clears the memory block

 ̄綄美尐妖づ 提交于 2019-12-01 10:34:43
问题 I am using DllImport to call method in c wrapper library from my own .net class. This method in c dll creates a string variable and returns the pointer of the string. Something like this; _declspec(dllexport) int ReturnString() { char* retval = (char *) malloc(125); strcat(retval, "SOMETEXT"); strcat(retval, "SOMETEXT MORE"); return (int)retval; } Then i read the string using Marshall.PtrToStringAnsi(ptr). After i get a copy of the string, i simply call another c method HeapDestroy which is

import c++ dll to windows phone project

时光怂恿深爱的人放手 提交于 2019-12-01 09:18:37
I'm new on windows phone developement and i have one problem that i don't know how to resolve.... the problem is.... i have a c++ project that i had complided with visual c++ 2010 and this create one dll with code compiled... so i know that C# import dll libraries but when i add refrences it's make this error "Unable to retrieve assembly fullname ""Parameter name: AssemblyPath" and i dont kown what it means... I searched on google and i found one method to import c++ dll manualy with DllImport and calling a external method... that causes one error because it's dont find the dll location... it

How do I handle a failed DllImport?

两盒软妹~` 提交于 2019-12-01 08:17:33
I'm attempting to write a C# managed class to wrap SHGetKnownFolderPath, so far it works on Vista, but crashes on XP due to not finding the proper function in shell32.dll, as expected. I want to have it set so I can fallback on a (admittedly hacky) solution using System.Environment.GetFolderPath if using XP. (Or, even better, if it can't find the funciton in shell32.) Is there any way to do this other then conditional compilation? My current code looks like: public abstract class KnownFolders { [DllImport("shell32.dll")] private static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType

Method's type signature is not PInvoke compatible while calling DLL method

半腔热情 提交于 2019-12-01 07:48:53
问题 I have a DLL with interface struct modeegPackage { uint8_t version; // = 2 uint8_t count; // packet counter. Increases by 1 each packet uint16_t data[6]; // 10-bit sample (= 0 - 1023) in big endian (Motorola) format uint8_t switches; // State of PD5 to PD2, in bits 3 to 0 }; __declspec(dllexport) void __cdecl initSerial(); __declspec(dllexport) void __cdecl closeSerialPort(); __declspec(dllexport) struct modeegPackage __cdecl getPackage(); And C# adapter class EEGCommunication { [StructLayout

Using C++ DLL in C# project

那年仲夏 提交于 2019-12-01 05:13:34
I got a C++ dll which has to be integrated in a C# project. I think I found the correct way to do it, but calling the dll gives me this error: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) This is the function in the dll: extern long FAR PASCAL convert (LPSTR filename); And this is the code I'm using in C# namespace Test{ public partial class Form1 : Form { [DllImport("convert.dll", SetLastError = true)] static extern Int32 convert([MarshalAs(UnmanagedType.LPStr)] string filename); private void button1_Click

inconsistent dll linkage & definition of dllimport static data member not allowed

好久不见. 提交于 2019-12-01 03:53:27
Assuming I have these two files: Header.h class DLL ExportClass{ public: ExportClass(); static int test; }; Source.cpp #ifdef EXPORT #define DLL __declspec(dllexport) #else #define DLL __declspec(dllimport) #endif #include "Header.h" int ExportClass::test = 0; ExportClass::ExportClass(){ } And I won't define EXPORT (to import a already exported class with a static member), why do I get these warnings: 1>source.cpp(11): warning C4273: 'test' : inconsistent dll linkage 1> header.h(4) : see previous definition of 'public: static int ExportClass::test' 1>source.cpp(13): warning C4273: 'ExportClass

Implicit vs. Explicit linking to a DLL

青春壹個敷衍的年華 提交于 2019-12-01 02:27:24
问题 When one should implicitly or explicitly link to a DLL and what are common practices or pitfalls? 回答1: It is fairly rare to explicitly link a DLL. Mostly because it is painful and error prone. You need to write a function pointer declaration for the exported function and get the LoadLibrary + GetProcAddress + FreeLibrary code right. You'd do so only if you need a runtime dependency on a plug-in style DLL or want to select from a set of DLLs based on configuration. Or to deal with versioning,