This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

How to Load External DLL from Dissector

0

I have a DLL file named, Test2.dll which loads fine in anything but wireshark, but when I try to open it within my wireshark dissector it fails to load, this is how I am loading it:

#define LIBRARYLOCATION "C:\\Docum...\\Test2.dll"

static void dissect_foo(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree){ HINSTANCE DLLInstance = LoadLibrary((LPCWSTR)LIBRARYLOCATION);

if(DLLInstance == NULL){ proto_tree_add_text(tree, NULL, 0, 0, "Cannot Load DLL: %*s", 0, LIBRARYLOCATION);} else{ proto_tree_add_text(tree, NULL, 0, 0, "Loaded DLL: %*s", 0, LIBRARYLOCATION);}

The .h file for the DLL is thus:

#ifndef TEST2_H
#define TEST2_H

#ifdef __cplusplus

extern "C" { #endif __declspec(dllexport) void Foo (); #ifdef __cplusplus } #endif #endif

Any ideas as to why this wouldn’t work? The DLL is compiled in VC++ 2005 (No option there) and the dissector is compiled with VC++ 2010 EE.

Thank you for your time, Brandon

asked 25 Jul ‘11, 11:56

officialhopsof's gravatar image

officialhopsof
318812
accept rate: 100%

edited 25 Jul ‘11, 17:57

helloworld's gravatar image

helloworld
3.1k42041

What is the error returned from the LoadLibrary call? To get extended error info call GetLastError() as per the MSDN page for LoadLibrary.

(25 Jul ‘11, 13:24) grahamb ♦

ERROR_MOD_NOT_FOUND
126 (0x7E)


is the error I get, so this points to a path issue? Is that possible since I am using an absolute path?

(25 Jul ‘11, 14:16) officialhopsof


One Answer:

1

Bug: You're passing a single-char string as a wide-char string to LoadLibrary, but you actually need to pass in a TCHAR string (which is set at compile-time to be either wide-char or single-char).

Fix: Use _T for the literal #define, and remove the LPCWSTR cast (presumably done to mute the compiler warning you should've heeded).

from:

#define LIBRARYLOCATION "C:\\Docum...\\Test2.dll"
/* ... */
HINSTANCE DLLInstance = LoadLibrary((LPCWSTR)LIBRARYLOCATION);

to:

//#include <tchar.h>
#define LIBRARYLOCATION _T("C:\\Docum...\\Test2.dll")
/* ... */
HINSTANCE DLLInstance = LoadLibrary(LIBRARYLOCATION);

answered 25 Jul '11, 17:55

helloworld's gravatar image

helloworld
3.1k42041
accept rate: 28%

That did the trick! I also really liked how you included the example!

Thanks! Brandon

(26 Jul '11, 07:06) officialhopsof

Good spot, I missed the cast.

More portably you could define the string literal as a wide character e.g. L"\path\to\your\dll".

No need to use Windows generic types then.

(26 Jul '11, 10:00) grahamb ♦

Using L would cause the same problem if UNICODE was not defined (in which case LoadLibrary expects a single-char string instead). If portability were a goal, I would use GLib's dynamic module loading (designed to be cross-platform).

(26 Jul '11, 11:24) bstn