CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2004
    Posts
    64

    'PMYDATA' : undeclared identifier - My first thread (a crap shoot)

    Hey,

    I am creating a dll that is used in one of my visual basic programs. The thread will just do some basic background calculations for me

    Code:
    #include "stdafx.h"
    #include "vbThreaded.h"
    #include <windows.h>
    
    UINT CVbThreadedApp::ThreadFunc()
    {
    	typedef void (__stdcall *FUNCPTR)(BSTR pbstr);
         FUNCPTR vbFunc;
    
         // Point the function pointer at the passed-in address.
         vbFunc = (FUNCPTR)addr;
    
         // Call the function through the function pointer.
         vbFunc(SysAllocString(L"Threaded call"));
    
    	return 0;
    }
    
    
    extern "C" short PASCAL EXPORT thread(long cbAddress)
    {
    	AFX_MANAGE_STATE(AfxGetStaticModuleState());
    	// normal function body here
        /* typedef void (__stdcall *FUNCPTR)(BSTR pbstr);
         FUNCPTR vbFunc;
    
         // Point the function pointer at the passed-in address.
         vbFunc = (FUNCPTR)cbAddress;
    
         // Call the function through the function pointer.
         vbFunc(SysAllocString(L"Hi! This message came from your DLL!"));
    	*/
    
    	addr = cbAddress;
    
    	PMYDATA pData;
    
    	// Allocate memory for thread data.
    
        pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                sizeof(MYDATA));
    	
    	pData->val1 = 1;
        pData->val2 = 100;
    
        hThread[i] = CreateThread( 
            NULL,              // default security attributes
            0,                 // use default stack size  
            ThreadFunc,        // thread function 
            pData,             // argument to thread function 
            0,                 // use default creation flags 
            &dwThreadId[i]);   // returns the thread identifier 
    
    	//myThread = AfxBeginThread(ThreadFunc,0);
    
    	return 0;
    }
    And since this is my first first threaded project I am getting some very newbist errors

    --------------------Configuration: vbThreaded - Win32 Debug--------------------
    Compiling...
    vbThreaded.cpp
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(99) : error C2065: 'PMYDATA' : undeclared identifier
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(99) : error C2146: syntax error : missing ';' before identifier 'pData'
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(99) : error C2065: 'pData' : undeclared identifier
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(104) : error C2065: 'MYDATA' : undeclared identifier
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(104) : error C2440: '=' : cannot convert from 'void *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(106) : error C2227: left of '->val1' must point to class/struct/union
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(107) : error C2227: left of '->val2' must point to class/struct/union
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(109) : error C2065: 'hThread' : undeclared identifier
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(109) : error C2065: 'i' : undeclared identifier
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(109) : error C2109: subscript requires array or pointer type
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(112) : error C2065: 'ThreadFunc' : undeclared identifier
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(115) : error C2065: 'dwThreadId' : undeclared identifier
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(115) : error C2109: subscript requires array or pointer type
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(115) : error C2102: '&' requires l-value
    Error executing cl.exe.

    vbThreaded.obj - 14 error(s), 0 warning(s)
    I tried using #include <strsafe.h> but the compiler just couldn't find the file (fatal error C1083: Cannot open include file: 'strsafe.h': No such file or directory)

    I was just wondering if anyone could help me get back on my feet?

  2. #2
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147

    Re: 'PMYDATA' : undeclared identifier - My first thread (a crap shoot)

    Derek buddy,

    Look, I don't want to start off all negative or discouraging, but the code you've got here is complete gibberish, clearly pieced together from snippets here and there.

    Now I'm all for jumping in feet first to try and figure out the code and what's going on, but what you have here is indicating basic syntax errors in the code.

    Clearly you're fairly new at C coding, not that that's a bad thing (I encourage it), but the topic of your efforts - to provide worker thread functionality running in the background is a fairly advanced topic. One that you really shouldn't venture into without a solid understanding of the problems associated with multi-threaded programming.

    Now, that said, you certainly have options. For example, you may have heard of the option within VB to use a timer as a pseudo thread, handling some background task that way.

    If you insist on using Visual C, then one approach might be to start with simple calls into a C DLL from VB (there are several examples of doing just this within this forum).

    ...this last item might be easier to call the DLL from a C/C++ application first, to get a feel for it, and to avoid the interfacing issues between C/VB, but that's your option.

    Once you are familiar with this technique (which is not without its own problems), you could tackle worker threads.

    If I was you, I'd become familiar with worker threads within a simple C application first to see how they work before leaping into embedding this within a DLL and calling it from VB - it's just not going to work first time.

    Feel free to bounce specific questions in the various forums along the way, but what you are asking here is *way* to complex a challenge for someone struggling with basic syntax errors.

    Again, sorry. I don't mean to rain on your picnic

    Good luck,

    - Nigel

  3. #3
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147

    Re: 'PMYDATA' : undeclared identifier - My first thread (a crap shoot)

    ...and answering your specific question, PMYDATA is not defined anywhere. This does not appear to be a name that Microsoft would use, so it must be something within your own code (or wherever the sample code came from).

    It also appears you are attempting to call VB functions from the worker thread. This may prove very problematic too.

    Hope this helps,

    - Nigel

  4. #4
    Join Date
    Oct 2004
    Posts
    64

    Re: 'PMYDATA' : undeclared identifier - My first thread (a crap shoot)

    Quote Originally Posted by NigelQ
    ...and answering your specific question, PMYDATA is not defined anywhere. This does not appear to be a name that Microsoft would use, so it must be something within your own code (or wherever the sample code came from).
    check out
    http://msdn.microsoft.com/library/de...ng_threads.asp for an example of PMYDATA

    It also appears you are attempting to call VB functions from the worker thread. This may prove very problematic too.
    Thats the general idea, as the title states - its a crap shoot. I'm just doing this on a wim

  5. #5
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147

    Re: 'PMYDATA' : undeclared identifier - My first thread (a crap shoot)

    The definition was right there too...

    Code:
    typedef struct _MyData {
        int val1;
        int val2;
    } MYDATA, *PMYDATA;
    Place this above your other code.

    Hope this helps,

    - Nigel

  6. #6
    Join Date
    Oct 2004
    Posts
    64

    Re: 'PMYDATA' : undeclared identifier - My first thread (a crap shoot)

    Thats what I get for codeing at 3am

    So, rereading everything, and then rewriting the code (I've removed the callbacks):

    Code:
    UINT CVbThreadedApp::ThreadFunc()
    {
    	return 0;
    }
    
    short CVbThreadedApp::thread(long cbAddress)
    {
    	AFX_MANAGE_STATE(AfxGetStaticModuleState());
    
    	DWORD dwThreadId;
        HANDLE hThread; 
    
        hThread = CreateThread( 
            NULL,              // default security attributes
            0,                 // use default stack size  
            ThreadFunc,        // thread function 
            0,				   // argument to thread function 
            0,                 // use default creation flags 
            &dwThreadId);   // returns the thread identifier 
    	
    	return 0;
    }
    The only error I get now is
    C:\Documents and Settings\Derek Shaw\Desktop\vbThreaded\vbThreaded.cpp(96) : error C2660: 'CreateThread' : function does not take 6 parameters
    This differs from the msdn documentation here

    The tooltip I get from vc++ is
    BOOL CreateThread(DWORD dwCreateFlags = 0, UINT nStackSize = 0)
    Last edited by Cerf; August 25th, 2005 at 02:56 PM.

  7. #7
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147

    Re: 'PMYDATA' : undeclared identifier - My first thread (a crap shoot)

    As you have it, the call is going to the CWinApp derined override of CreatThread, which does not have 6 parameters.

    What you need to do is to force the use of the global CreateThread function by placing a double colon before the call, so:
    Code:
        hThread = CreateThread(
    Becomes...
    Code:
        hThread = ::CreateThread(
    Hope this helps,

    - Nigel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured