0

I'm receiving following error:

Debug Assertion Failed!

Expression: string iterators incompatible

When trying to run such a code:

std::string string_Dir(){return ".\\Dir\\";}
std::wstring wstring_Dir=std::wstring(
    string_Dir().begin()
    ,string_Dir().end()
    );
SetDllDirectory(wstring_Dir.c_str());

Does someone know why

BTW: I followed this.

1
  • Because string != wstring, because char != wchar_t. Commented Apr 30, 2015 at 20:27

2 Answers 2

2

You are calling string_Dir() twice and then using iterators from different std::string objects to initialize your std::wstring. That is why you are getting an incompatibility error. You must use iterators from the same std::string object, so call string_Dir() once and assign the return value to a variable:

std::string dir = string_Dir();
std::wstring wstring_Dir(dir.begin(), dir.end());
SetDllDirectory(wstring_Dir.c_str());
// or better: SetDllDirectoryW(wstring_Dir.c_str());

That being said, you are not converting from ANSI to UTF-16, so this code will only work correctly if string_Dir() returns a std::string that contains only 7bit ASCII characters. It will fail if the std::string contains any non-ASCII 8bit characters.

There is a simpler solution - you can call SetDllDirectoryA() instead. You don't need the std::wstring, and the OS can do the ANSI-to-UTF16 conversion for you:

SetDllDirectoryA(string_Dir().c_str());
Sign up to request clarification or add additional context in comments.

Comments

1

According to the documentation, the value in the function call is supposed to be LPCTSTR instead of LPCTWSTR.

4 Comments

I'm using UNICODE #ifdef UNICODE #define SetDllDirectory SetDllDirectoryW
@user4838962 "To compile an application that uses this function, define _WIN32_WINNT as 0x0502 or later." Have you done this as well?
@user4838962 Also, wouldn't you call SetDllDirectoryWinstead of SetDllDirectory
@JeffS: Since UNICODE is defined, SetDllDirectory() maps to SetDllDirectoryW(). But since std::wstring is being used, it is better to call SetDllDirectoryW() directly and not rely on TCHAR mapping at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.