c# - P/Invoke AccessViolationException that makes no sense -


First of all, sorry to post a question when many other people have been asked about this topic, but I All my questions were being read (+ Google), and someone has given me any indication as to what is happening in my case.

I have a third party. DL (EnvelopeLAC), which has been exported, works with the same name:

  FLAC__StreamEncoderInitStatusFLAC__stream_encoder_init_file (FLAC__StreamEncoder * encoder, constant four * filename, FLAC__StreamEncoderProgressCallback progress_callback, zero * client_data) FLAC__StreamEncoderInitStatusFLAC__stream_encoder_init_FILE (FLAC__StreamEncoder * encoder, file * file, FLAC__StreamEncoderProgressCallback progress_callback, zero * Client_data) The aim of the first one is to start an encoder with a file name call it FOPIN () Which does the windows In some cases (with Unicode filenames), there is no good reason why this developer provides another function, which will open an open file * in the "w + b" mode (which I call MSVCRT.DLL _wfopen_s I have defined myself as P / Invite announcements in the form of:   

I am completely passing both of the same 1, 3 and 4 parameters, so I'm pretty sure that this problem is the IntPtr FileHandle parameter (3rd and 4th zeros and can be zero according to the docs Providing real value does not help either). I am also making sure that the actual file handle is ok: I am using the exact same code in another project _wfopen_s () and it is working perfectly, the file is also created before the crash, So there is no problem in it.

Edit: The value of the return is just a public Enum InitStatus: int.

For a moment, I thought there could be a problem in almost identical functions, so I tried to call them from Ordinal, but this does not work either: [DllImport (Globals.LIBFLAC_DLL, EntryPoint = "# 259", Charset = CharSet.Ansi)] / "FLAC__stream_encoder_init_file" OK [DllImport (Globals.LIBFLAC_DLL, EntryPoint = "# 258")] // "FLAC__stream_encoder_init_FILE" failed

I also thought that maybe the DLL I was using could have some kind of problem, so I It changed it to a compiled version by another 3 party (big in 150K size), but I get the same problem.

The code I am using to call:

  [TestMethod] Public Zero This Fill is Fun () {Object ClientData = null; IntPtr FileHandle = IntPtr.Zero; {FILE.Open (FileHandle Out, "test.flac", "w + b Try) Assign: Arnot (Interpreter. Zorro, FileHandle); // Works great! The file has been created, and the debugger file handle value shows the stream encoder. Int filehandle (flacostream encoder, fileholder, blank, ref client data); // AccessViolationException Assert.IsTrue (StreamEncoder.Finish (FlacStreamEncoder)); } Finally {FILE.Close (FileHandle); }} [TestMethod] Public verb this verse graded () {object ClientData = null; Confirm. Erequal (stream encoder. Instast. Oak, stream encoder. Entityfix (flaxstream encoder, "test.flax", blank, ref clintdata)); Assert.IsTrue (StreamEncoder.Finish (FlacStreamEncoder)); }  

PS: I do not know that this is the case, but I am using VS 2010 under V7,264.

Thank you for advance time

"I do not know that this is the case , But I'm using VS 2010 under Win 7x64. " - It matters a lot! Are you using a 32-bit libFLAC DLL? If so, in order to force the managed assembly to run in 32-bit mode, you must set your "platform target" under the Build tab of the project properties "x86", so it will match your 32-bit DLL The account is P-invoking

Or you can get a 64-bit libFLAC DLL, and then you have to include both 32-bit and 64-bit versions, and depending on the appropriate call IntPtr.Size The value of (to determine if you are running in 32- or 64-bit mode).


Comments