Valhalla Legends Forums Archive | C/C++ Programming | Creating a DLL part 2

AuthorMessageTime
Infamous
I have attemped to create a dll in C++ but I'm having some problems while Compiling it.

heres my code.

[code]
#include "stdafx.h"
#include "bitfieldclass.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved
)
{
    return TRUE;
}


// constructors
BitFields::BitFields(BYTE *d)
{
m_Data = d;
m_Pos = 0;
}

BitFields::BitFields()
{
m_Pos = 0;
}



DWORD BitFields::GetBitField( DWORD pos, DWORD len)
{
DWORD dBitField = (DWORD)(*(unsigned __int64 *)(m_Data+pos/8)<<(64-len-(pos&7))>>(64-len));
return dBitField ;
}



DWORD __stdcall CreateBitfields()
{
    return (DWORD)new BitFields;
}

void __stdcall DestroyBitfields(DWORD handle)
{
    delete (BitFields*)handle;
}

DWORD __stdcall GetBitField(DWORD handle, DWORD pos, DWORD len)
{
    return ((BitFields*)handle)->GetBitField(pos, len);
}

//bitfieldclass.h
class BitFields
{
public:
BYTE* m_Data;
DWORD m_Pos;

BitFields(BYTE *d) ;
BitFields() ;

DWORD GetBitField( DWORD pos, DWORD len) ;

} ;
[/code]

[code]
//.def file
LIBRARY "BitField"
DESCRIPTION "BitField DLL"

EXPORTS
  CreateBitfields @1
  DestroyBitfields @2
  GetBitField @3
[/code]

this is my output while Compiling

[code]
Compiling...
bitfield.cpp
Linking...
.\BitField.def : warning LNK4022: cannot find unique match for symbol "GetBitField"
.\BitField.def : warning LNK4002: "unsigned long __stdcall GetBitField(unsigned long,unsigned long,unsigned long)" (?GetBitField@@YGKKKK@Z) defined in .\Release\bitfield.obj
.\BitField.def : warning LNK4002: "public: unsigned long __thiscall BitFields::GetBitField(unsigned long,unsigned long)" (?GetBitField@BitFields@@QAEKKK@Z) defined in .\Release\bitfield.obj
LINK : fatal error LNK1152: cannot resolve one or more undecorated symbols
LINK : fatal error LNK1141: failure during build of exports file
Error executing link.exe.

bitfield.dll - 2 error(s), 3 warning(s)
[/code]

not sure what the problem is..please help

thanks.
November 26, 2005, 4:59 AM
Kp
[quote author=teK link=topic=13346.msg135352#msg135352 date=1132981194][code]
Linking...
.\BitField.def : warning LNK4022: cannot find unique match for symbol "GetBitField"
.\BitField.def : warning LNK4002: "unsigned long __stdcall GetBitField(unsigned long,unsigned long,unsigned long)" (?GetBitField@@YGKKKK@Z) defined in .\Release\bitfield.obj
.\BitField.def : warning LNK4002: "public: unsigned long __thiscall BitFields::GetBitField(unsigned long,unsigned long)" (?GetBitField@BitFields@@QAEKKK@Z) defined in .\Release\bitfield.obj
LINK : fatal error LNK1152: cannot resolve one or more undecorated symbols[/code]not sure what the problem is..please help[/quote]

The linker tells you exactly what's wrong if you read your own error messages.  You've given it ambiguous directions, and it cannot resolve them on its own.  You need to tell it which GetBitField you want (or rename one of them so as to avoid ambiguity in the first place).
November 26, 2005, 5:27 AM
Infamous
Thanks for the help Kp.

Now I have another problem, when calling the DLL from my VB app, I keep getting an error: Run-time error '49': Bad DLL calling convention.

Heres my functions to call it:

[code]
Public Declare Function CreateBitfields Lib "Bitfield.dll" (Handle As Long) As Long
Public Declare Sub DestroyBitfields Lib "Bitfield.dll" (Handle As Long)
Public Declare Function GetBitField Lib "Bitfield.dll" (Handle As Long, pos As Long, length As Long) As Long
[/code]

November 26, 2005, 1:25 PM
Kp
[quote author=teK link=topic=13346.msg135370#msg135370 date=1133011553]
Now I have another problem, when calling the DLL from my VB app, I keep getting an error: Run-time error '49': Bad DLL calling convention.[/quote]

I generally don't touch VB code, but this is usually caused by a calling convention mismatch with respect to the stack.  That is, VB wants your functions to be _stdcall, but for some reason they aren't (they're probably _cdecl instead).  From the code you originally posted, it looks like they should be _stdcall already.  Try adding _stdcall to the member declarations as well in your C++ source, and repost the latest copy if that still doesn't work.
November 26, 2005, 5:23 PM
Infamous
*Fixed*

The problem was from the IDE and not the code. If compiled to .EXE, the code works just fine  ;)

For more information
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q153586
November 26, 2005, 6:08 PM
Adron
[quote author=teK link=topic=13346.msg135389#msg135389 date=1133028538]
*Fixed*

The problem was from the IDE and not the code. If compiled to .EXE, the code works just fine  ;)

For more information
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q153586
[/quote]

Quote from that article:

[quote]
The fact that the EXE version allows you to call such functions has been confirmed to be a bug by Microsoft. You should not rely on this behavior as this might change in future versions of Visual Basic.
[/quote]

It is a problem with the dll. You are not supposed to "fix" it by compiling it to an exe.
November 26, 2005, 8:26 PM

Search