Solved

I need a dll source code in VC++ which should use some .lib file

Posted on 2001-07-16
20
381 Views
Last Modified: 2012-06-27
Hi,

Actually I have one .lib file and this I can't directly use in VB 6.0, so for that I have to create one dll in VC++.

Pls. note the .lib file's parameter type should be "(BYTE *psk, DWORD *psk_len)".  Using this u can give any example.  And also I want to know the declaration in VB.
For ex:- "Private Declare Function MyEncryptPSKey Lib "C:\test.dll" (?,?) As Long"

I need a VERY SIMPLE dll source code in VC++ which should use one library(.lib) file atleast.

I need the detailed source code with comments.  For this I am giving more points.

Thanx
Ramani.
0
Comment
Question by:ramani_gr
  • 10
  • 7
  • 3
20 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6287864
Your question is EXTREMELY unclear.


Do you want to create some sort of DLL that is an interface to the /lib file?  i..e a DLL that experts functions that coorespond to function in the library file and whose functions simply call the libarary file functions to do the work?

That would be pretty easy...  If that is what you want.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6287879
#include <windows.h>
#define SomeLib_Source
#include "SomeLib.h"

// Declaration of the function in the library file
extern LONG MyEncryptPSKey(BYTE *psk, DWORD *psk_len);


// The interfact function to be called by VB.
__declspec(dllexport) __stdcall LONG Do_MyEncryptPSKey(BYTE *psk, DWORD *psk_len)
{
   return MyEncryptPSKey(psk,psk_len);
};

// Sample DLLMain.  Used for intialization and clean up.  Can be ignord if not needed.
BOOL APIENTRY DllMain(HANDLE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved)
{
    return TRUE;
}

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 6287906
Opps you can ignore the

#define SomeLib_Source
#include "SomeLib.h"

lines.  Those got copied in by mistake.

AHHHH.  I forgot about the name decoration.  Lets try again.  How about


#include <windows.h>

// Declaration of the function in the library file
extern LONG MyEncryptPSKey(BYTE *psk, DWORD *psk_len);
// NOTE THE ABOVE LINE MIGHT NEED TO BE
// extern 'C' LONG MyEncryptPSKey(BYTE *psk, DWORD *psk_len);
// I CAN'T SAY WITHOUT KNOWING MORE ABOUT YOUR LIBRARY.

// The interfact function to be called by VB.
__declspec(dllexport) __stdcall extern "C" LONG Do_MyEncryptPSKey(BYTE *psk, DWORD *psk_len)
{
   return MyEncryptPSKey(psk,psk_len);
};

#pragma comment( linker, "/EXPORT:Do_MyEncryptPSKey")

// Sample DLLMain.  Used for intialization and clean up.  Can be ignord if not needed.
BOOL APIENTRY DllMain(HANDLE hModule,
                              DWORD  ul_reason_for_call,
                              LPVOID lpReserved)
{
   return TRUE;
}


Then when you compile the code you need to link it to the .lib file that defines  MyEncryptPSKey.


Note that depending on factors that are not made clear in your question, there may be soem changes that are necessary.  For example, where did this .lib come from?  What languge was it written in?  
0
 
LVL 1

Author Comment

by:ramani_gr
ID: 6288023
Hi,

Thanx for answering this.  See I have a lib file called test.lib.  That has this function "EncryptPSKey(BYTE *psk, DWORD *psk_len)"

Where exactly we are including this lib file in ur code.  Could u pls add this also in ur code.

And also I would like to know from vb what type of data value i have to pass to this dll function when I am using this dll from VB.  I want the declaration part and also the calling code in VB.

Cheers
Ramani.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6288214
>> Where exactly we are including this lib file in ur code.
No where.  a lib file is already compiled.  You must link to it.  It does not "appear" in the source code.

>> type of data value i have to pass to this dll function when
>> I am using this dll from VB.
Its impossible to say without knowing mroe about the function in the lib file.

VB doesn't really support pointers and the parameters are pointers, so there's probalby a bit of a problem there.  It might be possible to get aroudn that by making a thicker wrapper functon, but that is impossible to do without more information.

there's veyr little more that anyone cal tell you without more information aobut the lib and its functions.
0
 
LVL 1

Author Comment

by:ramani_gr
ID: 6288299
but there should be a way to include the lib file in the header file.  I am not sure, but in the .h file i saw the .lib file was imported using "import" or something like that.

if not in code, how to include this .lib file in vc++ environment.

now when i compile ur source code it gave me some errors and warnings.  I hope probably it would be because of that .lib file.

these were the errors & warnings.
warning C4518: 'extern ' : storage-class or type specifier(s) unexpected here; ignored
C:\Ramani\test\t\t.cpp(15) : warning C4230: anachronism used : modifiers/qualifiers interspersed, qualifier ignored
C:\Ramani\test\t\t.cpp(15) : error C2059: syntax error : 'string'
C:\Ramani\test\t\t.cpp(16) : error C2143: syntax error : missing ';' before '{'
C:\Ramani\test\t\t.cpp(16) : error C2447: missing function header (old-style formal list?)
C:\Ramani\test\t\t.cpp(53) : error C2084: function 'int __stdcall DllMain(void *,unsigned long,void *)' already has a body

And for ur information I haven't link the .lib file.  Probably this could cause these things.

How do i do that in vc++ environment.

Cheers
Ramani.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6289311
>> but there should be a way to include the lib file in the header file
No.   Its not source code.  Is already compiled code.   The compiler can't understand it.

You can add a line that tells the linker to automatically link to the file like

#pragma comment(lib,"C:\\somedirectory\\thelibfilename.lib")

This tells the linker to link to the file.  However, its often easier just to leave this out and add the .lib file to the project so that VC knows to link to it from the project settings.

>> how to include this .lib file in vc++ environment.
Open up the project manager window.  Right cliick on the project you want to add the .lib to.  Select "add files to project" from the pop-up menu that appears.  then in the dialog box that appears, find the lib file and press "ok"   (Note the dialog will probably default to searching for ".cpp" files, so you won't see the .lib file.  Choose a different file extension (.lib) at the bottom so you can see the .lib file.)


Try

// Declaration of the function in the library file
LONG MyEncryptPSKey(BYTE *psk, DWORD *psk_len);
// NOTE THE ABOVE LINE MIGHT NEED TO BE
// extern "C" LONG MyEncryptPSKey(BYTE *psk, DWORD *psk_len);
// I CAN'T SAY WITHOUT KNOWING MORE ABOUT YOUR LIBRARY.

Buit I still can't tall you which of those 2 lines is right.  Yoiu need to tell me where this ..lib came from.  Was langauge was it written in?  Was it made to be called from C or from C++ or from neither?  

Apparently the order is important here so change

__declspec(dllexport) __stdcall extern "C" LONG Do_MyEncryptPSKey(BYTE *psk, DWORD *psk_len)

to

extern "C" __declspec(dllexport) LONG__stdcall  Do_MyEncryptPSKey(BYTE *psk, DWORD *psk_len)
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6289522
Instead of all this why don't you create an new COM object

Statically link your lib to the project
Project---Settings----Link----General
Setting: Object/Library modules - add the name of your lib

Project---Settings----Link---Input
Setting: Additional Library Path - Add the path where your lib resides

Add a new COM interface to your COM object with what ever name you want.

Add a new method to execute your library call

In this method make your call to the library.

Are you passing back data to VB ? If you are then
Note you will have to cast your DWORD to a long as DWORD is a type not supported by  VB.

This will give you access to your lib through VB.

Hope this helps.

Vin.


0
 
LVL 1

Author Comment

by:ramani_gr
ID: 6290127
Hi Nietod

Now i got this code from u
****************************************************
#include <windows.h>

#pragma comment(lib,"C:\\ramani\\vc\\IrePolPr.lib")

// Declaration of the function in the library file
extern 'C' LONG EncryptPSKey(BYTE *psk, DWORD *psk_len);
// NOTE THE ABOVE LINE MIGHT NEED TO BE
// extern 'C' LONG MyEncryptPSKey(BYTE *psk, DWORD *psk_len);
// I CAN'T SAY WITHOUT KNOWING MORE ABOUT YOUR LIBRARY.

// The interfact function to be called by VB.
extern "C" __declspec(dllexport) __stdcall LONG Do_MyEncryptPSKey(BYTE *psk, DWORD *psk_len)
{
  return EncryptPSKey(psk,psk_len);
};

#pragma comment( linker, "/EXPORT:Do_MyEncryptPSKey")
// Sample DLLMain.  Used for intialization and clean up.  Can be ignord if not needed.

BOOL APIENTRY DllMain(HANDLE hModule,
                             DWORD  ul_reason_for_call,
                             LPVOID lpReserved)
{
  return TRUE;
}
****************************************************
When I compile this its giving the following error.
"fatal error C1010: unexpected end of file while looking for precompiled header directive"

Actually when I click the error its pointing the last line of this code.  I tried give extern "C" and extern 'C' and also I removed and tried.  Nothing works.  It gave me this error.

So could you please compile and give the code.

And Hi VincentLawlor,

Thanks for ur comments,  I have a .lib file, I need a dll out of it.  Actually I don't about VC++, but I still need a dll out of it to use that in VB.  B'coz i can't directly use this .lib file in VB.

So I need the code.  the exact vc++ code.

Thanks for helping me.
Ramani.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6290167
There is no way I can do this.  I can only ofer you advice.   But that advice is at best going to be poor advice since it seems I can't gather any new information from you.  With more information you might be ablt to get soem more help.  Without it...


I can say this much.

>> When I compile this its giving the following error.
>> "fatal error C1010: unexpected end of file while looking for precompiled header directive"
This error has nothing to do with the source code I wrote.  Its sounds like you are doing something funny with include files.  I can't guess what.   Did you do something with the .lib file that might cause this?

But you've got other problems that we can't help you with without knowing more about that .lib file.  If you want help you have to provide the infromation I asked for.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 22

Expert Comment

by:nietod
ID: 6290169
There is no way I can do this.  I can only ofer you advice.   But that advice is at best going to be poor advice since it seems I can't gather any new information from you.  With more information you might be ablt to get soem more help.  Without it...


I can say this much.

>> When I compile this its giving the following error.
>> "fatal error C1010: unexpected end of file while looking for precompiled header directive"
This error has nothing to do with the source code I wrote.  Its sounds like you are doing something funny with include files.  I can't guess what.   Did you do something with the .lib file that might cause this?

But you've got other problems that we can't help you with without knowing more about that .lib file.  If you want help you have to provide the infromation I asked for.
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6290186
Again go into project settings

Select C/C++

Choose Precompiled headers from the list have a look and see what files are using stdafx.h for any file using precompiled headers you must include this #include "stdafx.h".

Guess the COM idea is out then. VC++ will do most of the work for you.

Vin.
0
 
LVL 1

Author Comment

by:ramani_gr
ID: 6290303
I added this also now.  #include "stdafx.h"
but it still gave the following errors

C:\RamFolder\VC\t1\t1\t1.cpp(7) : warning C4091: 'extern ' : ignored on left of 'int' when no variable is declared
C:\RamFolder\VC\t1\t1\t1.cpp(7) : error C2143: syntax error : missing ';' before 'constant'
C:\RamFolder\VC\t1\t1\t1.cpp(7) : fatal error C1004: unexpected end of file found

That .lib file is ok, it can't give these problem.
Ok.  now if you guys want me do say accept ur answers.  I can do that.
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6290317
Is stdafx the very first file you include ??
What are the contents of stdafx.cpp ?

Vin.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6290356
>> I added this also now.  #include "stdafx.h"
Why?

ramani_gr, do you want help?   If so we need to know more about this lib file.  If it was written in C instead of C++ we need to know.  if it wasn'e written in either C or C++ we need to know.  We need to know what this funciton does so we can make sense of the parmaeter, or at least anythign you can tell us about it.

We need to hear from you or this is just pointless.
0
 
LVL 1

Author Comment

by:ramani_gr
ID: 6290365
I added this also now.  #include "stdafx.h"
but it still gave the following errors

C:\RamFolder\VC\t1\t1\t1.cpp(7) : warning C4091: 'extern ' : ignored on left of 'int' when no variable is declared
C:\RamFolder\VC\t1\t1\t1.cpp(7) : error C2143: syntax error : missing ';' before 'constant'
C:\RamFolder\VC\t1\t1\t1.cpp(7) : fatal error C1004: unexpected end of file found

That .lib file is ok, it can't give these problem.
Ok.  now if you guys want me do say accept ur answers.  I can do that.
0
 
LVL 1

Author Comment

by:ramani_gr
ID: 6290410
I added this also now.  #include "stdafx.h"
but it still gave the following errors

C:\RamFolder\VC\t1\t1\t1.cpp(7) : warning C4091: 'extern ' : ignored on left of 'int' when no variable is declared
C:\RamFolder\VC\t1\t1\t1.cpp(7) : error C2143: syntax error : missing ';' before 'constant'
C:\RamFolder\VC\t1\t1\t1.cpp(7) : fatal error C1004: unexpected end of file found

That .lib file is ok, it can't give these problem.
Ok.  now if you guys want me do say accept ur answers.  I can do that.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6290700
>> I added this also now.  #include "stdafx.h"
>> but it still gave the following errors
WHY DID YOU ADD THAT?

I HAVE ASKED YOU MANY QUESTIONS NOW AND YOU HAVE NOT ANSWERED ANY OF THEM

WE CANNOT HELP YOU WITHOUT YOUR HELP.

>> That .lib file is ok, it can't give these problem.
NO ONE SAID IT WASN'T OKAY.  BUT ITS IMPOSSIBLE TO USE IT WITHOUT NMORE INFORMATION ABOUT IT.  THAT IS ONE OF THE THINGS i HAVE BEEN ASKING ABOUT.
0
 
LVL 1

Author Comment

by:ramani_gr
ID: 6290845
ok. leave that.  I don't have any idea about the .lib file.  My company got this file from other company.  Now I have to work on that .lib.  It seems i have to learn more about vc++,  I thought i can just get the code from u guys and can build a dll and then I can proceed with VB.  Hahahaha.

Never mind this.  ok.  Now tell me how to share the points to u guys.

Thanks for ur help
Ramani.
0
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 6290912
>> I don't have any idea about the .lib file.
That makes extermely difficult at best, impossible at worst.  We don't even know the calling convention of the code.  That can make it hard to link.  it make be a lot of trial and error.  Even after it links it may be trial and error to get the code to call and return successfully.  The same thing aplies to the parameters.  You don't know how they are used so how do you know what to pass.

Do you know what language the file was written in?  Do you know the compiler?  Does it have any associated files that might provide clues  (like .h or .inc files).

What does the function do?  That might help you to figure out the parameters although you still might have a lot of guess work to do.

Can yoiu contact the company?

>>  i have to learn more about vc++
Learning more about C++ will help, but until you have the answers to these questions, you won't have success.  It may be that the only way to find the answers is trial and error, but that shoudl be the last resort.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now