2016-08-23 12 views
0

私はwin32apiを使ってWindows実行可能ファイルをコンパイルしようとしましたが、多くのロードブロックが発生しました。cygwinでwinapiをコンパイルします

最初に試してみたのは、man gcc|grep -e '-mno-cygwin'が成功し、mingwがインストールされているため、gcc: error: unrecognized command line option '-mno-cygwin'が奇妙な結果となったgcc -mno-cygwin [...]です。

インクルードしようとしている他のヘッダーファイルに関係なく、私はいつも不満足な循環インクルード依存関係に終わっています(a.hではb.hにa.hが必要です)。 Win32 APIに基づいて実行可能ファイルを適切にコンパイルするために必要なヘッダーファイルのセットは何ですか?私が試してみました

ヘッダファイル:

  • w32api/WINNT.H
  • w32api/DDK/ntddk.h
  • w32api /はWindows.h
  • w32api/ntdef.h

ソースコード:

$ cat source.c 
#include <w32api/winnt.h> 
#include <w32api/ddk/ntddk.h> 

int main(int argc, char **argv) { 
    PHANDLE hProcess; 
    CLIENT_ID processId; 
    processId.UniqueProcess = (HANDLE) 1000; 
    processId.UniqueThread = 0; 

    NtOpenProcess(&hProcess, PROCESS_VM_READ|PROCESS_VM_WRITE, 0, &processId); 
    return 0; 
} 

$ gcc -o a.exe -I/usr/include/w32api -I/usr/include/w32api/ddk source.c 
In file included from memoryEdit.c:1:0: 
/usr/include/w32api/winnt.h:291:11: error: unknown type name 'CONST' 
    typedef CONST WCHAR *LPCWCH,*PCWCH; 
     ^
[...] 

$ gcc --version 
gcc (GCC) 5.4.0 
$ uname -a 
CYGWIN_NT-6.1 HOST_NAME 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin 
+0

* "Windows.h" *は標準Windowsアプリケーション用です。ただし、[NtOpenProcess](https://msdn.microsoft.com/en-us/library/windows/hardware/ff567022.aspx)はWindows APIの一部ではありません。 * Ntddk.h *または* Ntifs.h *をインクルードし、残りの部分を処理しなければなりません。 Windows APIを使用していない理由は[OpenProcess](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684320.aspx)です。 – IInspectable

+0

@Inspectable 'Ntifs.h'は私のシステムのどこにも見つからないので、'/dtk/ntifs.h'を意味すると仮定します。その結果、 '/ usr/x86_64-w64- = mingw32/sys-root/mingw/include/ddk/Ntifs.h:32:19:致命的なエラー:ntddk.h:そのようなファイルやディレクトリはありません。 'NtOpenProcess'を使うことについてはあまり気にしませんでした。hello worldスタイルのサンプルプログラムをコピーして貼り付け、少なくともコンパイルすることを期待していました。 –

+0

@IInspectable gccコマンドに '-I/usr/x86_64-w64-mingw32/sys-root/mingw/include/ddk'を追加したところ、' ddk/ntifs.h'を見つけることができました。エラー '/usr/x86_64-w64-mingw32/sys-root/mingw/include/ddk/wdm.h:376:1:エラー: 'Windows.h'から' _InterlockedAdd64 'の再定義が含まれていますが、削除した場合それから私は 'error:unknown type name 'BYTE'という文字を残して、私の元の質問を残すだけです。 –

答えて

0

gcc -mno-cygwinは何年も前に削除されました。

Windowsプログラムをコンパイルするには、gccコンパイラではなく、cygwinからcygwinに、クロスコンパイラcygwinでwindowsを使用する必要があります。 2パッケージ変異体はアーチに応じてあります

にMinGW64-i686の-gccの
にMinGW64-x86_64の-gccのを

+0

これは半分の質問にお返事ありがとうございます。だから私は別の質問をします。 –

0

私は、Eclipse/GCCでコンパイルしてちょうど今何の問題もなかったです。ここに私のテストファイルがあります:

/* 

============================================================================ 
Name  : win32gcctest.c 
Author  : Clark Thomborson 
Version  : 1.0 
Copyright : Copyleft 
Description : Testing winapi synchronous file access within Cygwin 
============================================================================ 
*/ 

#include <stdio.h> 
#include <windows.h> 
#include <stdlib.h> 
#include <fileapi.h> 

void my_write(char* fname) { 
    HANDLE hFile; 
    char DataBuffer[] = "This is some test data to write to the file."; 
    DWORD dwBytesToWrite = (DWORD) strlen(DataBuffer); 
    DWORD dwBytesWritten = 0; 
    BOOL bErrorFlag = FALSE; 

    hFile = CreateFile(fname,  // name of the write 
      GENERIC_WRITE,   // open for writing 
      0,      // do not share 
      NULL,     // default security 
      CREATE_ALWAYS,   // overwrite any existing file 
      FILE_ATTRIBUTE_NORMAL, // normal file 
      NULL);     // no attr. template 

    if (hFile == INVALID_HANDLE_VALUE) { 
     DWORD last_err = GetLastError(); 
     printf("Error code %d: unable to open file \"%s\" for write.\n", last_err, fname); 
     exit(last_err); 
    } 

    printf("Writing %d bytes to %s.\n", dwBytesToWrite, fname); 

    bErrorFlag = WriteFile(hFile, 
      DataBuffer,  // start of data to write 
      dwBytesToWrite, // number of bytes to write 
      &dwBytesWritten, // number of bytes that were written 
      NULL);   // no overlapped structure 

    if (FALSE == bErrorFlag) { 
     DWORD last_err = GetLastError(); 
     printf("Error code %d: unable to write to file \"%s\".\n", last_err, fname); 
     exit(GetLastError()); 
     exit(last_err); 
    } else { 
     if (dwBytesWritten != dwBytesToWrite) { 
      // This is an error because a synchronous write that results in 
      // success (WriteFile returns TRUE) should write all data as 
      // requested. This would not necessarily be the case for 
      // asynchronous writes. 
      printf("Error: dwBytesWritten != dwBytesToWrite\n"); 
      exit(EXIT_FAILURE); 
     } else { 
      printf("Wrote %d bytes to %s successfully.\n", dwBytesWritten, fname); 
     } 
    } 

    CloseHandle(hFile); 
} 

HANDLE my_open_for_read(char* fname) { 
    HANDLE hFile; 

    hFile = CreateFile(
     fname, 
     GENERIC_READ, 
     FILE_SHARE_DELETE, // another process may delete this file while this handle is open 
     NULL, // no security attributes 
     OPEN_EXISTING, // returns error if file not found 
     FILE_ATTRIBUTE_NORMAL, // no special attributes, so can't do async IO 
     NULL // no attributes will be copied from another file 
     ); 
    if (hFile == INVALID_HANDLE_VALUE) { 
     DWORD last_err = GetLastError(); 
     printf("Error code %d: unable to open file \"%s\" for read.\n", last_err, fname); 
     exit(last_err); 
    } 

    return hFile; 
} 

ssize_t my_read(HANDLE hFile, void *buffer, size_t bytes_to_read) { 

    DWORD bytes_read; 

    if (ReadFile(hFile, buffer, bytes_to_read, &bytes_read, NULL)) { 
     return (ssize_t) bytes_read; 
    } else { 
     DWORD last_err = GetLastError(); 
     printf("Error code %d: unable to read file.\n", last_err); 
     exit(last_err); 
    } 
} 

int main(void) { 
    char fname[32] = "test.txt"; 

    my_write(fname); 

    printf("Reading %s.\n", fname); 

    char buff[1024]; 
    HANDLE hFile = my_open_for_read(fname); 
    ssize_t nc = my_read(hFile, buff, 1023); // allow room for terminating byte 
    if(nc >= 0) { 
     buff[nc] = 0; // terminate string 
     printf("Read %d characters: %s", (int) nc, buff); 
     return EXIT_SUCCESS; 
    } else { // buggy my_read() 
     printf("Error %d", (int) nc); 
     return nc; 
    } 
} 
関連する問題