c++ - Building a DLL with VC++ function is inaccessible -
today needed small library recursively list files in folder , subfolders. decided create small library, simple , contains 2 functions:
bool __cdecl isdir(std::string dir); void __cdecl listdir(std::string dir, std::vector<std::string> &files, bool recursive);
the function of these quiet self explanatory.
it contains 1 header file defines class fr
, export using
__declspec( dllexport )
the reason me bothering small library can used in future projects without having incorporate source files projects time.
this how try call 1 of functions:
fr *clazz = new fr(); clazz->isdir("c:/path/to/dir");
and error generated:
intellisense: function "fr::isdir" (declared @ line 11 of "c:\dev\filerecursiondll\inc\fr.h") inaccessible
[fr.cpp]
#include "fr.h" using namespace std; bool apientry dllmain(handle hmodule, dword ul_reason_for_call, lpvoid lpreserved) { return true; } dllexport bool __cdecl fr::isdir(string dir){ struct stat fileinfo; stat(dir.c_str(), &fileinfo); if (s_isdir(fileinfo.st_mode)){ return true; } else{ return false; } } dllexport void __cdecl fr::listdir(string dir, vector<string> &files, bool recursive){ dir *dp; //create directory object struct dirent *entry; //create entry structure dp = opendir(dir.c_str()); //open directory converting string const char* if (dir.at(dir.length() - 1) != '/'){ dir = dir + "/"; } if (dp != null){ //if directory isn't empty while (entry = readdir(dp)){ //while there in directory if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0){ //and if entry isn't "." or ".." if (isdir(dir + entry->d_name) == true && recursive == true){//check if new path directory, , if (and recursion specified true), recurse. files.push_back(string(entry->d_name)); //add entry list of files listdir(dir + entry->d_name, files, true); //recurse } else{ files.push_back(string(entry->d_name));//add entry list of files } } } (void)closedir(dp); //close directory } else{ perror("couldn't open directory."); } }
[fr.h]
#ifndef fr_h #define fr_h #define dllexport __declspec( dllexport ) #include<dirent.h> #include<vector> class dllexport fr { bool __cdecl isdir(std::string dir); void __cdecl listdir(std::string dir, std::vector<std::string> &files, bool recursive); }; #endif
note: reference library doing appreciated!
thanks in advance!
class
members private
default, struct
members public
default.
that said, why not general , simple create , use header-only module instead of adding complexity of compiler-specific dll.
also, windows programming better use wide character strings (i.e. unicode).
example:
#pragma once // copyright (c) 2014 alf p. steinbach #undef unicode #define unicode #include <windows.h> #include <string> // std::wstring #include <vector> // std::vector namespace filesystem{ using std::wstring; using std::vector; inline auto path_join( wstring const& a, wstring const& b ) -> wstring { int const len_a = a.length(); int const len_b = b.length(); if( len_a == 0 ) { return b; } if( len_b == 0 ) { return a; } wstring result = a; if( a[len_a - 1] == l'\\' ) { if( b[0] == l'\\' ) { result.resize( len_a - 1 ); } } else if( b[0] != l'\\' ) { result += l'\\'; } result += b; return result; } enum recursion { no_recursion, recursive }; inline auto directory_entries( wstring const& directory_path, recursion const r = no_recursion ) -> vector<wstring> { vector<wstring> result; win32_find_data state = {}; handle const h = findfirstfile( path_join( directory_path, l"*.*" ).c_str(), &state ); for( bool more = (h != invalid_handle_value); more; more = !!findnextfile( h, &state ) ) { result.push_back( state.cfilename ); if( r == recursive && !!(state.dwfileattributes & file_attribute_directory) ) { // todo: check isn't "." or ".." or (hard problem) circular symlink entry. bool const is_true_subdirectory = false; // <-- here. if( is_true_subdirectory ) { auto const sub_entries = directory_entries( path_join( directory_path, state.cfilename ), recursive ); for( auto const& e : sub_entries ) { result.push_back( e ); } } } } findclose( h ); return result; } } // namespace filesystem
Comments
Post a Comment