need to get a function pointer to strtok in the DLL

This is a discussion on need to get a function pointer to strtok in the DLL within the C++ Programming forums, part of the General Programming Boards category; so far i have Code: HMODULE MSVCRT = LoadLibrary("MSVCRT.DLL"); if(MSVCRT == NULL) { MessageBox(NULL,"MSVCRT module failed","",0); } else { MessageBox(NULL,"MSVCRT ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    need to get a function pointer to strtok in the DLL

    so far i have
    Code:
    HMODULE MSVCRT = LoadLibrary("MSVCRT.DLL");
    if(MSVCRT == NULL)
    {
    MessageBox(NULL,"MSVCRT module failed","",0);
    }
    else
    {
    MessageBox(NULL,"MSVCRT module loaded","",0);
    GetProcAddress(MSVCRT,"strtok");
    }
    from here i dont know how to go about getting a function pointer to strtok so my thread can use it at runtime and not keep crashing

    thx
    Last edited by Anddos; 02-03-2009 at 07:56 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,680
    What let you down this path to begin with?

    gg

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    129
    Quote Originally Posted by Codeplug View Post
    What let you down this path to begin with?

    gg
    Yes good question. Why the heck are you trying to dynamically load something from one of the standard libraries? Why not just link to the routine in the normal fashion or link to a library that calls the DLL?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like all others: Surely, just including string.h and usign strtok() like any other person would do is fine for you too?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,251
    Quote Originally Posted by Anddos View Post
    from here i dont know how to go about getting a function pointer to strtok so my thread can use it at runtime and not keep crashing
    Aaaack. You said "thread" and "strtok" in the same sentence. That's like putting matter and antimatter together.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Does that have something to do with strtok relying on global state? Is the only way to use strtok in multiple threads by ensuring that no two threads try to tokenize a string at a same time.

    Incidentally this is a C++ thread. Are you sure that there are no C++ solutions, such as std::stringstream (boost has tokenizers for more sophisticated needs)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,251
    Quote Originally Posted by anon View Post
    Does that have something to do with strtok relying on global state?
    Yes, although it's probably not "global" in the sense of a global variable -- more likely, strtok() caches the input position in a static local variable. It's still not reentrant, and probably shouldn't be used even in single threaded code.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    Yes, although it's probably not "global" in the sense of a global variable -- more likely, strtok() caches the input position in a static local variable. It's still not reentrant, and probably shouldn't be used even in single threaded code.
    Indeed. This will ALSO fail:
    Code:
    char *otherfunc(char *str);
    int somefunc(char *str ...) 
    {
        char *ptr = strtok(str, separators);
        while(ptr)
        {
            char *tmp = otherfunc(ptr);
            ...
            ptr = strtok(NULL, separators);
        }
        ...
    }
    
    char *otherfunc(char *str)
    {
        return strtok(str, ".-:;");
    }
    This code will fail even if there are no threads!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't know much about DLLs, but if you load strtok from "MSVCRT.DLL" and the strtok that you get when you just use it normally links to the same function in the same dll, is there any reason to believe that they will use a different static variable under the hood?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. any suggestion - pointer, strtok, function etc
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-13-2002, 01:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21