c# - Access violation on c++ methods launched from DllImport -
i have strange problem methods, launched dllimport.
in native c++ have code:
#define bufsize 4096 int _tmain(int argc, _tchar* argv[]) { wchar_t *tekst = l"d:\\matiz\\dokumenty\\!firma trim-pot\\firmowe"; printtekst(tekst); } bool printtekst(wchar_t *tekst) { dword retval = 0; bool success; tchar buffer[bufsize] = text(""); tchar buf[bufsize]; tchar** lpppart = { null }; retval = getfullpathnamew(tekst, bufsize, buffer, lpppart); return true; } bool __stdcall printtekstextern(wchar_t *tekst) { return printtekst(tekst); } when call printtekst method direct main function ok, when call method using dllimport, on line:
tchar buffer[bufsize] = text(""); i have access violation exception. it's strange situation, because in fact don't use arguments, come manager code in case.
method, launched in c#:
[dllimport("pointers.exe", callingconvention = callingconvention.stdcall, charset = charset.auto)] public static extern bool printtekstextern(string tekst); static void main(string[] args) { string cos = @"d:\matiz\dokumenty\visual studio 2013\projects\pointers\pointerssharp\bin\debug"; var status = printtekstextern(cos); } }
could me?
the big problem trying import function executable rather dll. that's first thing fix, , real cause of fatal crash.
beyond that, unmanaged function is:
bool __stdcall printtekstextern(wchar_t *tekst) your p/invoke is:
[dllimport("...", callingconvention = callingconvention.stdcall, charset = charset.auto)] public static extern bool printtekstextern(string tekst); there 1 minor problem here. return value marshalled 4 byte boolean type, unmanaged code uses 1 byte type. read here: http://blogs.msdn.com/b/jaredpar/archive/2008/10/14/pinvoke-and-bool-or-should-i-say-bool.aspx
fix changing p/invoke so:
[dllimport("...", callingconvention = callingconvention.stdcall, charset = charset.auto)] [return: marshalas(unmanagedtype.u1)] public static extern bool printtekstextern(string tekst); i suggest using charset.unicode since unmanaged code uses wchar_t , therefore explicit using 16 bit text.
Comments
Post a Comment