0

i have problem with this code here

C++ DLL

#include <iostream>
#include <windows.h>
#include <boost/filesystem.hpp>
#include <boost/thread/thread.hpp>
using namespace std;
using namespace boost::filesystem;
extern "C"
{
__declspec(dllexport) void IMSS_GetDirs(LPCSTR *value)
{

    directory_iterator iterator(string("D:\\"));
    for(; iterator != directory_iterator(); ++iterator)
    {
        *value = iterator->path().filename().string().c_str(); // Thats don't work , the c_str() convert to LPCSTR
        MessageBoxA(NULL,iterator->path().filename().string().c_str(),"TEST",MB_OK); // Thats also work
    }
    *value = "Test"; // Thats Work 
}
}

And the VB.NET Part

    Private Declare Sub IMSS_GetDirs Lib "IMSS_SDFCCore.dll" _
   (ByRef value As String)

    Private Sub Button1_Click() Handles Button1.Click
        IMSS_GetDirs(Label1.Text)
    End Sub

So why when its get the value directly its work and when its get it by the directory_iterator does not work ?

Al.Pertro
  • 175
  • 1
  • 6
  • What is your DLL supposed to do that .NET cannot? – Alexander Jan 20 '14 at 10:50
  • The .net can do that but not fast as the c++ code. – Al.Pertro Jan 20 '14 at 10:51
  • `*value` is declared as `LPCSTR`, the C stands for constant. Maybe it's read-only? Check this out: http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr – Alexander Jan 20 '14 at 11:25
  • That's abject nonsense, the cost here is in iterating the directory. That doesn't have anything to do with the language you use to call the operating system function. You'll have to learn a lot more about C++ before you can get this right. Particularly about the *undefined behavior* that your code invokes. That string variable is gonzo when your function returns. The pinvoke marshaller trying to destroy it will also not come to a good end. – Hans Passant Jan 20 '14 at 11:26
  • Hans Passant, How do you get this right if you want to do it ? – Al.Pertro Jan 20 '14 at 11:32

0 Answers0