0

Currently struggling with c++ as a I mostly been working with c# in the past. But this is my code:

    template<typename fn> fn InterfaceManager::GetInterface(std::string modulename, std::string interfacename)
{
    CreateInterfaceFn CreateInterface;
    ASSERT(CreateInterface =(CreateInterfaceFn)GetProcAddress(GetModuleHandle(modulename), "CreateInterface"));

    fn pInterface = 0;
    for (int i = 100; i > 0; i--)
    {
        std::stringstream intf;
        intf << interfacename << std::setfill('0') << std::setw(3) << i;
        pInterface = (fn)(CreateInterface(intf.str().c_str(), 0));
        if (pInterface) { 
            Log::Write(DEBUG,"%s found 0x%.8X", intf.str().c_str(), pInterface); 
            break;
        }
    }

    return pInterface;
} 

but I am getting the following error and I am not sure how to solve it

cannot convert argument 1 from 'std::string' to 'LPCSTR'

Foo Bar
  • 55
  • 1
  • 7
  • `GetModuleHandle(modulename.c_str())`. – BoBTFish Mar 04 '16 at 16:26
  • LPCSTR is just a pointer to a NUL-terminated array of characters—in other words, a C-style string. Don't let the name confuse you. Do note that you should not be nesting these function calls. They can fail, in which case they will return NULL, and you should be checking for and handling those failure conditions. – Cody Gray - on strike Mar 04 '16 at 16:26
  • `TCHAR` may be `char` or `wchar_t`. It depends on your project settings. To use `std` with `TCHAR`, you will have to define new types: `typedef std::basic_string< TCHAR > tstring;`, for instance. If you use MFC, the simples conversion would be `CString mfcs( stds.c_str() );` `CString` will work with most WAPI functions. – zdf Mar 04 '16 at 16:49
  • One more thing: you have passed the arguments by value `(std::string modulename...`. To pass the arguments by reference use `(const std::string& modulename...`. – zdf Mar 04 '16 at 16:53

0 Answers0