Search code examples
c++excelvisual-studio-2010filesystemobjectxlw

What's the next step after #import when using Scripting.FileSystemObject in C++?


Context: Windows 7, XLW 5.x, Microsoft Excel 2007, Visual Studio 2010, C++

Given the following bit of code ...

#import "c:\windows\system32\scrrun.dll" raw_interfaces_only, \
raw_native_types, \
named_guids, \
rename("DeleteFile", "_DeleteFile"), \
rename("MoveFile","_MoveFile"), \
rename("CopyFile", "_CopyFile"), \
rename("GetFreeSpace", "_GetFreeSpace")
using namespace Scripting;

How do I now instantiate the FileSystemObject and call its methods?


Solution

  • This worked for me

    std::wstring fileExists(std::wstring name)
    {
        VARIANT_BOOL b = 0;
        ::CoInitialize(NULL);
        {
            CComPtr<IFileSystem> spFSO;
            HRESULT hr = spFSO.CoCreateInstance(L"Scripting.FileSystemObject");
            if (SUCCEEDED(hr) && spFSO)
            {
                BSTR theName = SysAllocStringLen( name.data(), name.size());
                hr = spFSO->FileExists(theName, &b);
            }
        }
        ::CoUninitialize();
        return (b == -1) ? L"Exists" : L"Doesn't exist";
    }
    

    That at least got me connected to the functionality. There's still the getting it right for the situation, but that's beyond the scope of the posting.