April 12, 2005 Re: calling C functions findfirst etc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle Attachments: | On Mon, 11 Apr 2005 23:08:15 -0400, Ben Hinkle <ben.hinkle@gmail.com> wrote:
> "Regan Heath" <regan@netwin.co.nz> wrote in message news:opso2eamu423k2f5@nrage.netwin.co.nz...
>> I am trying to call the C function findfirst. It fills a struct with
>> file
>> information.
>>
>> I have converted the parts of the C header io.h that I believe are
>> required.
>> It links, it runs, it gets garbage.
>>
>> I have attached my code (b.d) and a test done in C (test.c).
>>
>> Running both you can see the address and size of each struct member involved.
>>
>> Anyone know what I am doing wrong?
>>
>> Regan
>
> Googling for "digitalmars findfirst" turns up some links that indicate
> the
> DMC++ version of findfirst isn't like the others. Maybe try
> "dos_findfirst"?
> I'm not really sure. Check out dm/include/dos.h for dos_findfirst.
Found it. Tried it. I cannot get it to link. I think I am missing the required lib. Grepping the lib dir for findfirst finds it in snn.lib, but dos_findfirst is not found in any lib.
Attached is my current test case.
Regan
|
April 12, 2005 Re: calling C functions findfirst etc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | In article <opso2eamu423k2f5@nrage.netwin.co.nz>, Regan Heath says... > >------------G5FaL5SajqwBDVSW69Khb2 >Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 Content-Transfer-Encoding: 8bit > >I am trying to call the C function findfirst. It fills a struct with file information. > >I have converted the parts of the C header io.h that I believe are >required. >It links, it runs, it gets garbage. > >I have attached my code (b.d) and a test done in C (test.c). > >Running both you can see the address and size of each struct member involved. > >Anyone know what I am doing wrong? > >Regan Regan maybe this code would be helpful, here's a ported example I've done using Windows' Win32 API FindFirstFile() function. # /+ # MSDN Online Library - FindFirstFile Example # http://msdn.microsoft.com/library/default.asp? # url=/library/en-us/fileio/base/findfirstfile.asp # # #define _WIN32_WINNT 0x0400 # # #include <windows.h> # #include <stdio.h> # # int main(int argc, char *argv[]) # { # WIN32_FIND_DATA FindFileData; # HANDLE hFind; # # printf ("Target file is %s.\n", argv[1]); # hFind = FindFirstFile(argv[1], &FindFileData); # if (hFind == INVALID_HANDLE_VALUE) # { # printf ("Invalid File Handle. GetLastError reports %d\n", # GetLastError ()); # return (0); # } # else # { # printf ("The first file found is %s\n", FindFileData.cFileName); # FindClose(hFind); # return (1); # } # } # +/ # # // Win32 API example of FindFirstFile() # private import std.c.windows.windows; # private import std.string; # private import std.stdio; # # int main( char[][] args ) # { # WIN32_FIND_DATA FindFileData; # HANDLE hFind; # # if ( args.length <= 1 ) # { # writefln( "Parameter missing, try the following... # FindFirstFile \"C:\\Test.txt\"" ); # return 1; # } # # hFind = FindFirstFileA( args[ 1 ], &FindFileData ); # # if ( hFind == INVALID_HANDLE_VALUE ) # { # writefln( "File not found or Invalid File Handle. # GetLastError reports %d", GetLastError() ); # return 0; # } # else # { # writefln( "The first file found is %s", # toString( FindFileData.cFileName ) ); # # //writefln( "%08x %d", data.dwFileAttributes, # data.dwFileAttributes.sizeof ); # FileTimeToSystemTime( &data.ftCreationTime, lpSystemTime); # writefln( "Created=%02d/%02d/%04d %d", # systime.wDay, systime.wMonth, systime.wYear, # data.ftCreationTime.sizeof ); # # FileTimeToSystemTime( &data.ftLastAccessTime, lpSystemTime); # writefln( "Last Access=%02d/%02d/%04d %d", # systime.wDay, systime.wMonth, systime.wYear, # data.ftLastAccessTime.sizeof ); # # FileTimeToSystemTime( & data.ftLastWriteTime, lpSystemTime); # # writefln( "Last Written=%02d/%02d/%04d %d", # systime.wDay, systime.wMonth, systime.wYear, # data.ftLastWriteTime.sizeof ); # # writefln( "%08x %d", data.nFileSizeLow, # data.nFileSizeLow.sizeof ); # writefln( "%s %d", toString( data.cFileName ), # data.cFileName.sizeof ); # # FindClose( hFind ); # return 1; # } # # return 0; # } Output in the WinXP console: ---------------------------- C:\dmd>dmd findfirstfile.d C:\dmd\bin\..\..\dm\bin\link.exe findfirstfile,,,user32+kernel32/noi; C:\dmd>findfirstfile "C:\test.txt" The first file found is Test.txt C:\dmd>findfirstfile "C:\*.*" The first file found is 1DPR00714.MTF C:\dmd>findfirstfile "C:\*.txt" The first file found is Support Issue.txt C:\dmd>findfirstfile "C:\*.lic" File not found or Invalid File Handle. GetLastError reports 2 C:\dmd> David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" |
April 12, 2005 Re: calling C functions findfirst etc | ||||
---|---|---|---|---|
| ||||
Posted in reply to David L. Davis | Opps!! Please add these inside main() SYSTEMTIME systime; SYSTEMTIME* lpSystemTime = &systime; I had commented the main code with "#"s and then I decided to add more code and thought I had added all my changes to it, but it looks like I missed these. Sorry about that. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" |
April 12, 2005 Re: calling C functions findfirst etc | ||||
---|---|---|---|---|
| ||||
Posted in reply to David L. Davis | Ok...it looks like the lazy way of "cut and paste" is not going to save me any time or work. :( I've just recommented the current working version...sorry everybody for number of posts its take, just to get a simple example posted correctly. I think I'll go hide under a rock now, until tomorrow. # /+ # MSDN Online Library - FindFirstFile Example found at: # http://msdn.microsoft.com/library/default.asp # ?url=/library/en-us/fileio/base/findfirstfile.asp # # #define _WIN32_WINNT 0x0400 # # #include <windows.h> # #include <stdio.h> # # int main(int argc, char *argv[]) # { # WIN32_FIND_DATA FindFileData; # HANDLE hFind; # # printf ("Target file is %s.\n", argv[1]); # hFind = FindFirstFile(argv[1], &FindFileData); # if (hFind == INVALID_HANDLE_VALUE) # { # printf ("Invalid File Handle. GetLastError reports %d\n", # GetLastError ()); # return (0); # } # else # { # printf ("The first file found is %s\n", FindFileData.cFileName); # FindClose(hFind); # return (1); # } # } # +/ # # // Win32 API example of FindFirstFile() # private import std.c.windows.windows; # private import std.string; # private import std.stdio; # # int main( char[][] args ) # { # WIN32_FIND_DATA data; # HANDLE hFind; # SYSTEMTIME systime; # SYSTEMTIME* lpSystemTime = &systime; # # if ( args.length <= 1 ) # { # writefln( "Parameter missing, try the following... # FindFirstFile \"C:\\Test.txt\"" ); # return 1; # } # # hFind = FindFirstFileA( args[ 1 ], &data ); # # if ( hFind == INVALID_HANDLE_VALUE ) # { # writefln( "File not found or Invalid File Handle. # GetLastError reports %d", GetLastError() ); # return 0; # } # else # { # writefln( "The first file found is %s", toString( data.cFileName ) ); # //writefln( "%08x %d", data.dwFileAttributes, # data.dwFileAttributes.sizeof ); # # FileTimeToSystemTime( &data.ftCreationTime, lpSystemTime); # writefln( "Created=%02d/%02d/%04d %d", # systime.wDay, systime.wMonth, systime.wYear, # data.ftCreationTime.sizeof ); # # FileTimeToSystemTime( &data.ftLastAccessTime, lpSystemTime); # writefln( "Last Access=%02d/%02d/%04d %d", # systime.wDay, systime.wMonth, systime.wYear, # data.ftLastAccessTime.sizeof ); # # FileTimeToSystemTime( & data.ftLastWriteTime, lpSystemTime); # # writefln( "Last Written=%02d/%02d/%04d %d", # systime.wDay, systime.wMonth, systime.wYear, # data.ftLastWriteTime.sizeof ); # # writefln( "%08x %d", data.nFileSizeLow, # data.nFileSizeLow.sizeof ); # writefln( "%s %d", toString( data.cFileName ), # data.cFileName.sizeof ); # # FindClose( hFind ); # return 1; # } # # return 0; # } Output in the WinXP Console: ---------------------------- C:\dmd>dmd findfirstfile.d C:\dmd\bin\..\..\dm\bin\link.exe findfirstfile,,,user32+kernel32/noi; C:\dmd>findfirstfile "C:\test.txt" The first file found is Test.txt Created=12/04/2005 8 Last Access=12/04/2005 8 Last Written=12/04/2005 8 0000000e 4 Test.txt 260 C:\dmd>findfirstfile "C:\*.*" The first file found is 1DPR00714.MTF Created=03/02/2005 8 Last Access=01/04/2005 8 Last Written=03/02/2005 8 000048d2 4 1DPR00714.MTF 260 C:\dmd>findfirstfile "C:\*.txt" The first file found is Support Issue.txt Created=01/12/2004 8 Last Access=18/01/2005 8 Last Written=01/12/2004 8 0001147d 4 BeginNew Support Issue.txt 260 C:\dmd>findfirstfile "C:\*.lic" File not found or Invalid File Handle. GetLastError reports 2 C:\dmd> David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" |
April 12, 2005 Re: calling C functions findfirst etc | ||||
---|---|---|---|---|
| ||||
Posted in reply to David L. Davis | Thanks David. This works, though I am still interested in why the other one fails.
Regan
On Tue, 12 Apr 2005 18:39:23 +0000 (UTC), David L. Davis <SpottedTiger@yahoo.com> wrote:
> Ok...it looks like the lazy way of "cut and paste" is not going to save me any
> time or work. :( I've just recommented the current working version...sorry
> everybody for number of posts its take, just to get a simple example posted
> correctly.
>
> I think I'll go hide under a rock now, until tomorrow.
>
> # /+
> # MSDN Online Library - FindFirstFile Example found at:
> # http://msdn.microsoft.com/library/default.asp
> # ?url=/library/en-us/fileio/base/findfirstfile.asp
> #
> # #define _WIN32_WINNT 0x0400
> #
> # #include <windows.h>
> # #include <stdio.h>
> #
> # int main(int argc, char *argv[])
> # {
> # WIN32_FIND_DATA FindFileData;
> # HANDLE hFind;
> #
> # printf ("Target file is %s.\n", argv[1]);
> # hFind = FindFirstFile(argv[1], &FindFileData);
> # if (hFind == INVALID_HANDLE_VALUE)
> # {
> # printf ("Invalid File Handle. GetLastError reports %d\n",
> # GetLastError ());
> # return (0);
> # }
> # else
> # {
> # printf ("The first file found is %s\n", FindFileData.cFileName);
> # FindClose(hFind);
> # return (1);
> # }
> # }
> # +/
> #
> # // Win32 API example of FindFirstFile()
> # private import std.c.windows.windows;
> # private import std.string;
> # private import std.stdio;
> #
> # int main( char[][] args )
> # {
> # WIN32_FIND_DATA data;
> # HANDLE hFind;
> # SYSTEMTIME systime;
> # SYSTEMTIME* lpSystemTime = &systime;
> #
> # if ( args.length <= 1 )
> # {
> # writefln( "Parameter missing, try the following...
> # FindFirstFile \"C:\\Test.txt\"" );
> # return 1;
> # }
> #
> # hFind = FindFirstFileA( args[ 1 ], &data );
> #
> # if ( hFind == INVALID_HANDLE_VALUE )
> # {
> # writefln( "File not found or Invalid File Handle.
> # GetLastError reports %d", GetLastError() );
> # return 0;
> # }
> # else
> # {
> # writefln( "The first file found is %s", toString( data.cFileName ) );
> # //writefln( "%08x %d", data.dwFileAttributes,
> # data.dwFileAttributes.sizeof );
> #
> # FileTimeToSystemTime( &data.ftCreationTime, lpSystemTime);
> # writefln( "Created=%02d/%02d/%04d %d",
> # systime.wDay, systime.wMonth, systime.wYear,
> # data.ftCreationTime.sizeof );
> #
> # FileTimeToSystemTime( &data.ftLastAccessTime, lpSystemTime);
> # writefln( "Last Access=%02d/%02d/%04d %d",
> # systime.wDay, systime.wMonth, systime.wYear,
> # data.ftLastAccessTime.sizeof );
> #
> # FileTimeToSystemTime( & data.ftLastWriteTime, lpSystemTime);
> #
> # writefln( "Last Written=%02d/%02d/%04d %d",
> # systime.wDay, systime.wMonth, systime.wYear,
> # data.ftLastWriteTime.sizeof );
> #
> # writefln( "%08x %d", data.nFileSizeLow,
> # data.nFileSizeLow.sizeof );
> # writefln( "%s %d", toString( data.cFileName ),
> # data.cFileName.sizeof );
> #
> # FindClose( hFind );
> # return 1;
> # }
> #
> # return 0;
> # }
>
> Output in the WinXP Console:
> ----------------------------
> C:\dmd>dmd findfirstfile.d
> C:\dmd\bin\..\..\dm\bin\link.exe findfirstfile,,,user32+kernel32/noi;
>
> C:\dmd>findfirstfile "C:\test.txt"
> The first file found is Test.txt
> Created=12/04/2005 8
> Last Access=12/04/2005 8
> Last Written=12/04/2005 8
> 0000000e 4
> Test.txt 260
>
> C:\dmd>findfirstfile "C:\*.*"
> The first file found is 1DPR00714.MTF
> Created=03/02/2005 8
> Last Access=01/04/2005 8
> Last Written=03/02/2005 8
> 000048d2 4
> 1DPR00714.MTF 260
>
> C:\dmd>findfirstfile "C:\*.txt"
> The first file found is Support Issue.txt
> Created=01/12/2004 8
> Last Access=18/01/2005 8
> Last Written=01/12/2004 8
> 0001147d 4
> BeginNew Support Issue.txt 260
>
> C:\dmd>findfirstfile "C:\*.lic"
> File not found or Invalid File Handle. GetLastError reports 2
>
> C:\dmd>
>
> David L.
>
> -------------------------------------------------------------------
> "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
>
|
Copyright © 1999-2021 by the D Language Foundation