473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to use a third party dll

hi
i want to call some functions from a DLL(which is provided by a hardware manufacturer without the .lib and .def). I've used these following code


Expand|Select|Wrap|Line Numbers
  1.   HINSTANCE hinstLib; 
  2.     MYPROC ProcAdd=NULL; 
  3.     BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
  4.  
  5.     // Get a handle to the DLL module.
  6.  
  7.     hinstLib = LoadLibrary(TEXT("C:\My.dll")); 
  8.  
  9.     // If the handle is valid, try to get the function address.
  10.  
  11.     if (hinstLib != NULL) 
  12.     { 
  13.         std::cout<<"loaded\n";
  14.  
  15.  
  16.  
  17.  ProcAdd = (MYPROC) GetProcAddress(hinstLib, "funcinDLL");
  18.  
  19.  // If the function address is valid, call the function.
  20.  
  21.         if (NULL != ProcAdd) 
  22.         {
  23.             fRunTimeLinkSuccess = TRUE;
  24.             (*ProcAdd) (); 
  25.         }
  26.         // Free the DLL module.
  27.  
  28.         fFreeResult = FreeLibrary(hinstLib); 
  29.     } 
  30.  
  31.     // If unable to call the DLL function, use an alternative.
  32.     if (! fRunTimeLinkSuccess) 
  33.         printf("Message printed from executable\n"); 
  34.  
but LoadLybrary is failing to load the DLL..
can anyone suggest me , how can i do the job?
Apr 6 '10 #1
5 2754
Banfa
9,065 Expert Mod 8TB
When LoadLibrary fails it must be for a reason. Assuming that you have the path correct I suggest you call and print the return value from GetLastError to get some clarificaion of what is causing the problem.

You can look up the value returned from GetLastError in winerror.h
Apr 6 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
Your path to the dll must be C:\\My.dll. The \ is an escape character so you need \\ to get an actual \ in your path.

Also, the function name used by GetProcAddress must be exported by the DLL and if the DLL is written in C++ that must also be an extern "C" function.
Apr 6 '10 #3
hi
thanks for your reply.
i used the following code to get the last error code.

LPTSTR lpszFunction=TEXT("GetProcessId");
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
std::cout<<"enter2\n";
// Display the error message and exit the process

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
std::cout<<"enter3\n";
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
But it didn't print the error code
Apr 7 '10 #4
hi
thanks for your reply.
the DLL is in VC++. I didn't get that you have said about the path. The actual location of the dll is
C:\Documents and Settings\hexapod\Desktop\cyton_important\My.dll
i used this path in the LoadLibrary.
i kept a copy of the dll in project folder also.
Apr 7 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
You have to call GetLastError() immediately after the function:

Expand|Select|Wrap|Line Numbers
  1. AWindowsFunnction();
  2. DWORD dw = GetLastError();
to get the error from AWindowsFunction.


This path:

C:\Documents and Settings\hexapod\Desktop\cyton_important\My.dll

is seen by the compiler as:

C:ocuments and Settingsexapodesktopyton_importanty.dll

because the \ is the escape sequence. That means the character after the \ is a code and not a separate character. Like \n means newline character and not a \ followed by an n.

You have to code \\ to tell the compiler that the code is \.

Therefore:

C:\\Documents and Settings\\hexapod\\Desktop\cyton_important\\My.dll

compiles to:

C:\Documents and Settings\hexapod\Desktop\cyton_important\My.dll

which is what you want.
Apr 7 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chuck Chekuri | last post by:
Hi, We have a 3TB oracle db. 24x7 operation with data constantly coming in to the DB. Users from all over the world use a web based applicaton to query and work on the data. Out transaction...
0
by: David | last post by:
First, a question. Is there a way to keep VS.NET from red lining elements from third party controls with an error message that says 'the active schema does not support the element ****'? ...
1
by: Chris | last post by:
Does anyone have any documentation on compact privacy policy and asp.net session or http session cookies. I have a third party app that relies on a session cookie to maintain the state of my app...
0
by: ronnotel | last post by:
I have integrated APIs from a third party into my framework. The third party is essentially a .Net wrapper on top of an existing set of C++ classes and structs. I want to pass-around objects in...
6
by: Ben Finney | last post by:
Howdy all, I'm improving an existing application that's partly written using Python and the standard library. Many of the improvements I want to make can be done by using third-party free...
5
by: Daz | last post by:
Hi everyone, Sorry about the long thread name, I think that my problem is that I don't know how to word it, which is why I have been unsuccessful finding the information I require. I am...
4
by: m11533 | last post by:
I am developing a large application with Visual Studio .NET 2003 using C#. We recently added a new third party product with a native .NET library. I want to place this third party product's dll in...
0
by: =?Utf-8?B?b3h5Z2Vub25lQGdtYWlsLmNvbQ==?= | last post by:
Hey everyone. I am developing a C# program that launches various third-party applications (applications I do not have the source code to) with various inputs. Some of those inputs could cause the...
2
by: Marcus | last post by:
Good evening, I'm new to developing large subversion-controlled projects. This one will involve a few third-party libraries like wxWidgets, and perhaps Twisted. Ordinarily you could just install...
5
by: rohitchawla | last post by:
How is it possible to read the third party cookies if u have a valid p3p policy set in place I have a server side application that sets the third party cookie and whenever the client visit any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.