Link to home
Start Free TrialLog in
Avatar of ramani_gr
ramani_gr

asked on

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

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.
Avatar of nietod
nietod

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.
#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
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?  
Avatar of ramani_gr

ASKER

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.
>> 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.
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.
>> 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)
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.


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.
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.
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.
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.
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.
Is stdafx the very first file you include ??
What are the contents of stdafx.cpp ?

Vin.
>> 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.
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.
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.
>> 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.
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.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial