Calling to a pointer from a c++ thread

0

I'm trying to call from a thread to a pointer. Here is my code:

myDll.dll c++ :

long cbAddrAsync;
void _asyncer(void* data)
{
        typedef void (__stdcall *FUNCPTR)();
    FUNCPTR vbFunc;
    vbFunc = (FUNCPTR)cbAddrAsync;
    vbFunc();
}
extern "C" __declspec(dllexport) void async(long addr)
{
    cbAddrAsync = addr;
    HANDLE hHandle = (HANDLE)_beginthread(_asyncer,0,NULL);
}

calling to this extern with vb6: In Module1:

Declare Sub async Lib "myDll.dll" (ByVal addr As Long)
Sub onAsync()
MsgBox "ASYNC"
End Sub

In Form1:

Private Sub Command_Click()
Call async(AddressOf Module1.onAsync)
End Sub

when i click at command button , The problem occurs in

dll:
Unhandled exception at 0x734f9232 in Project1.exe: 0xC0000005: Access violation reading location 0x00000076.
in vbFunc();

How can I fix it? Thanks.

c++
multithreading
pointers
vb6
asked on Stack Overflow Sep 20, 2014 by Mohammad Drk • edited Sep 20, 2014 by ne1410s

1 Answer

0

0x00000076 is an invalid memory address. All addresses under 0x0000ffff are invalid. This catches a whole class of errors (failed memory allocations or object creation) along the lines of 0x0 and 0x0 + 2 (or 0x0 + 0x76) etc. The errors are errors that programmers are supposed to check for but often don't.

My guess is you are accessing the 0x76th byte of a structure that wasn't initialised.

VB6 is designed to be a COM server not a dynamic load dll.

AddressOf has rules because you are actually calling VB6 runtime not your program.

You can also start in a debugger.

windbg or ntsd (ntsd is a console program and maybe installed). Both are also from Debugging Tools For Windows.

Download and install Debugging Tools for Windows

http://msdn.microsoft.com/en-us/windows/hardware/hh852363

Install the Windows SDK but just choose the debugging tools.

Create a folder called Symbols in C:\

Start Windbg. File menu - Symbol File Path and enter

srv*C:\symbols*http://msdl.microsoft.com/download/symbols

then

windbg -o -g -G c:\windows\system32\cmd.exe /k batfile.bat

You can press F12 to stop it and kb will show the call stack (g continues the program). If there's errors it will also stop and show them.

Type lm to list loaded modules, x *!* to list the symbols and bp symbolname to set a breakpoint

If programming in VB6 then this environmental variable link=/pdb:none stores the symbols in the dll rather than seperate files. Make sure you compile the program with No Optimisations and tick the box for Create Symbolic Debug Info. Both on the Compile tab in the Project's Properties.

Also CoClassSyms (microsoft.com/msj/0399/hood/hood0399.aspx) can make symbols from type libraries.

answered on Stack Overflow Sep 20, 2014 by Noodles

User contributions licensed under CC BY-SA 3.0