Сборка DLL с функцией VC ++ недоступна

0

Сегодня мне нужна небольшая библиотека, чтобы рекурсивно перечислить все файлы в папке и во всех подпапках. Поэтому я решил создать небольшую библиотеку, это очень просто и содержит только две функции:

bool __cdecl isDir(std::string dir);
void __cdecl listDir(std::string dir, std::vector<std::string> &files, bool recursive);

Функция из них является спокойной самоочевидной.

Он содержит один заголовочный файл, который определяет класс FR и экспортирует, используя

__declspec (dllexport)

Причина, по которой я пытаюсь сделать эту небольшую библиотеку, заключается в том, что ее можно использовать в будущих проектах без необходимости постоянно включать исходные файлы в мои проекты.

Вот как я пытаюсь вызвать одну из функций:

FR *clazz = new FR();
clazz->isDir("C:/path/to/dir");

И это ошибка:
IntelliSense: функция "FR :: isDir" (объявлена в строке 11 "C:\dev\FileRecursionDLL\inc\fr.h") недоступна

[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 the directory object
    struct dirent *entry; //create the entry structure
    dp = opendir(dir.c_str()); //open directory by converting the string to const char*
    if (dir.at(dir.length() - 1) != '/'){
        dir = dir + "/";
    }
    if (dp != NULL){ //if the directory isn't empty
        while (entry = readdir(dp)){ //while there is something in the directory
            if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0){ //and if the entry isn't "." or ".."
                if (isDir(dir + entry->d_name) == true && recursive == true){//check if the new path is a directory, and if it is (and recursion is specified as true), recurse.
                    files.push_back(string(entry->d_name)); //add entry to the list of files
                    listDir(dir + entry->d_name, files, true); //recurse
                }
                else{
                    files.push_back(string(entry->d_name));//add the entry to the list of files
                }
            }
        }
        (void)closedir(dp); //close directory
    }
    else{
        perror("Couldn't open the 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

ПРИМЕЧАНИЕ. Также будет оценена ссылка на библиотеку, которая уже делает это.

Заранее спасибо!

  • 0
    Ваши методы isDir и listDir являются private (возможно, сделайте их public , я подозреваю, что это ошибка.
  • 1
    @Niall Да, это была ошибка, я был так близко: S
Показать ещё 7 комментариев
Теги:
dll
header-files
dllexport

1 ответ

1
Лучший ответ

члены class являются private по умолчанию, члены struct по умолчанию являются public.


Тем не менее, почему бы не сделать общее и простое создание и использование модуля только для заголовков вместо того, чтобы добавить сложность DLL для компилятора.

Кроме того, для программирования Windows лучше использовать широкие символьные строки (например, Unicode).


Пример:

#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 that this isn't a "." 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
  • 0
    Поскольку у меня мало знаний о модулях только для заголовков, я знаю, что dirent.h - автономный заголовочный файл, но я хочу создать / найти чрезвычайно простой API, поэтому я могу сделать это, просто вызвав одну функцию без необходимости копировать и вставьте его во все мои проекты.
  • 0
    @Linus: хорошо, тогда позволь мне приготовить что-нибудь.
Показать ещё 4 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню