SetFilePointer不会失败,但也不会移动指针

首先,请原谅我,如果我说些傻话,我不是电脑工程师的IT人员,但我被分配到需要更多技能的东西。

我需要在SD卡中写入和读取物理扇区。 我用C ++完成了它,但主要的应用程序是用C#编写的,所以我认为这是编写我的第一个dll的好时机。

以下是用于编写扇区的c ++代码。

private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) { HANDLE hFile = INVALID_HANDLE_VALUE; BOOL fSuccess = FALSE; DWORD dwBytesWritten = 0; unsigned char chBuffer[SIZE]; long SectorActual = 39; long PosicionInicio = SectorActual * 512; for (int i=0; iText += "Could not open file (error " + GetLastError() + ") \r\n"; return; } DWORD dwPtr = SetFilePointer( hFile, PosicionInicio, NULL, FILE_BEGIN ); if (dwPtr == INVALID_SET_FILE_POINTER) // Test for failure { textBox1->Text += "Error moving pointer (error " + GetLastError() + ") \r\n"; return; // Deal with failure } // End of error handler fSuccess = WriteFile(hFile, chBuffer, TAMANYO_SECTOR, &dwBytesWritten, NULL); if (!fSuccess) { textBox1->Text += "WriteFile failed\r\n"; } else { textBox1->Text += "WriteFile wrote " + dwBytesWritten.ToString() + " bytes\r\n"; } CloseHandle(hFile); } 

然后我制作了DLL。 function是这样的:

  int AccesoBajoNivel::EscribeBloqueFisico(char numeroDisco, unsigned char * buffer, long BytesQueEscribir, long posicion_inicio) { HANDLE hFile = INVALID_HANDLE_VALUE; BOOL fSuccess = FALSE; DWORD dwBytesWritten = 0; char ArrayDeChars[] = "\\\\.\\PhysicalDrive5"; ArrayDeChars[17] = numeroDisco; LPCSTR pathAlDisco = ArrayDeChars; hFile = CreateFileA(pathAlDisco,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile == INVALID_HANDLE_VALUE) { //printf("Could not open file (error %d)\n", GetLastError()); CloseHandle(hFile); return -1; } DWORD dwPtr = SetFilePointer( hFile, posicion_inicio, NULL, FILE_BEGIN ); if (dwPtr == INVALID_SET_FILE_POINTER) // Test for failure { CloseHandle(hFile); return -2; // Deal with failure } // End of error handler fSuccess = WriteFile(hFile, buffer, BytesQueEscribir, &dwBytesWritten, NULL); if (!fSuccess) { CloseHandle(hFile); return -3; } else { CloseHandle(hFile); //return dwBytesWritten; return dwPtr; } CloseHandle(hFile); } 

然后我在C#项目中导入:

 [DllImport("AccesoBajoNivel.dll", CallingConvention = CallingConvention.Cdecl)] static extern int EscribeBloqueFisico(char numeroDisco, byte[] buffer, long BytesQueEscribir, long posicion_inicio); 

然后我调用函数:

 int ResultEscribir = EscribeBloqueFisico(numeroDiscoFisico, BufferEscritura, 512, SectorEscribir * 512); 

总是写入第一个扇区,无论“posicion_inicio”的值(是起始偏移,抱歉西class牙语标签)这就是为什么我改变api中的写入函数返回dwPtr(SetFilePointer的结果)而不是写入的字节。 这似乎总是零,这显然不是我想要的。 但是找不到为什么第一个函数工作而dll调用没有。

也许是乍看之下应该看到的东西,但正如我所说,我是电子人,不习惯这种编程…

其他function(如读取DISK_GEOMETRY或VOLUME_DISK_EXTENTS)工作,我用它来确定哪个物理驱动器是给定的逻辑单元。 正如我所说,写作和阅读工作也很好,只是我总是指向零行业……

提前致谢。 这也是我第一次在这里发帖。 多次回头(这就是为什么我选择在这里问)但如果我在发布问题时犯了一些错误,请指出。

C ++中的long是32位。 这使它成为C#中的int 。 在pinvoke声明中用int替换long。

你应该得到一个PInvokeStackImbalance警告。 如果您将其关闭,请务必重新启用该警告。 并且很容易看出调试器中* posicion_inicio *的值是错误的。 确保启用非托管调试,Project + Properties,Debug选项卡。 现在,您可以在C ++代码中设置断点。