Hi, I'm a VB programmer who is trying to convert an old C console application to a C++ ATL COM DLL for use with a VB GUI. My code basically allows the client to set a filename and then call a function which launches a private function in a seperate thread and returns immediately. My private function then trundles along, raising events to inform the client of it's progress along the way. All sounds good so far, unfortunately my code doesn't work, all my properties, methods, variables and so on were created by wizards. Here is my implementation file (Asynch.cpp): #include "stdafx.h" #include "AsynchEg.h" #include "Asynch.h" // Required for _beginthread and _endthread - do not remove #include "process.h" #include "comdef.h" STDMETHODIMP CAsynch::RequestNewThread() { // Begin a new thread and start the _NewThread function _beginthread(_NewThread, 0, (void*)(this)); return S_OK; } void CAsynch::_NewThread(void* pCaller) { FILE *file; BSTR wfilename; char nfilename[256]; LPSTR filename; // Cast the passed void pointer to a CAsynch object CAsynch* pAsynch = (CAsynch*) pCaller; // Convert the FileName property to a character array for use in fopen pAsynch->get_FileName(&wfilename); WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, wfilename, -1, nfilename, sizeof nfilename , NULL, NULL); filename = nfilename; // Open the file if((file = fopen(filename, "rb")) == 0) { // TODO: Raise error } else { // Close the file fclose(file); // Tell the calling object to raise it's ThreadDone event pAsynch->Fire_ThreadDone(); } // End this thread _endthread(); } // Returns the FileName property STDMETHODIMP CAsynch::get_FileName(BSTR *pVal) { // Populate the pointer with the private variable *pVal = m_FileName; return S_OK; } // Sets the FileName property STDMETHODIMP CAsynch::put_FileName(BSTR newVal) { // Copy the new value to the private variable _bstr_t NewBSTR; NewBSTR.Assign(newVal); m_FileName = NewBSTR.copy(); return S_OK; } If I run my VB exe then I get a memory exception. If I run the C++ end in debug then I get an access violation on the Invoke line of the Fire_ThreadDone function. If I run VB in debug mode my code works and my event is raised. What is going on? If you need a copy of my projects (VC++6 and VB6) then mail me and I'll send you a copy. Also, can anyone tell me how to raise run time errors to my VB client? Any consttuctive criticism / advice is greatly appreciated. Best R
R
Rob Brown
@Rob Brown