Quote:
>I am trying to write a simple DLL in MS visual C(++) version 4.0 to be
>called by MS
Visual Basic Version 4.0.
>I've successfully generated the DLL in C, and have a test version program
>also written in C that successfully calls and exersize the DLL. However,
>when I attempt to link that into the Visual Basic program, I get an error
>49 "Bad DLL Calling Convention".
>This error comes out -after- the DLL has successfully completed running and
>returned. This seems to indicate that I might have the function type
>incorrectly defined (I have it defined as int in the C version and as Long
>in the Basic version.
>I've used the convention
> extern "C" __declspec( dllexport ) FunctionID (char *Img_File, char
>*Cmp_File, int Threshold, int Importance, int Left, int Top, int Right, int
>Bottom)
>as the method to define the DLL routine interfaces, but I have also tried
>using WINAPI, FAR Pascal, and other combinations of things all with
>differing degrees of effectiveness with the C version of the calling
>program, but all failures with the Basic version.
>Is there something else I need to do to get this to work correctly with
>Visual Basic?
I had the same problem. This is how I implement the .CPP file to
create a DLL. I'm attaching an example DLL and corresponding VB
file.. I'm only including the code for the FontTest and LineTest
functions in C; it shows you how to pass an hDC to a DLL function
and then use it as one.
It's not fancy or high-faluten', but it'll do the job (and a lot
quicker than VB would! damn!)
LineTest draws a bunch-o' lines to the window;
FontTest draws rotated text to a window at a specific spot..
Modify it to do your own bidding!
// CHRIS
// Cosmic Computers, Inc.
In SOMEFILE.DLL:
#include <windows.h>
#include <ole2.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
// Declaration decoration!
#define DLLAPI __declspec(dllexport)
// Examples of Declarations
DLLAPI BSTR _stdcall cml_UCase (BSTR);
DLLAPI short int _stdcall cml_OnlyDigits(BSTR);
DLLAPI BOOL _stdcall cml_strcmp(BSTR, BSTR);
DLLAPI UINT _stdcall cml_sizeof(int);
DLLAPI void _stdcall cml_clipboundary(long int, long int, long int,
long int);
DLLAPI void _stdcall cml_clipShadow();
DLLAPI void _stdcall cml_clipFromShadow();
DLLAPI int _stdcall cml_clipsegment(long int *, long int *, long
int *, long int *);
DLLAPI void _stdcall cml_LineTest (long int, long int, long int, long
int, long int);
DLLAPI void _stdcall cml_FontTest(long, long, long, int);
//.================================================================
//
// Test function for drawing into a device context. Units are Pixels,
// not twips!
//
DLLAPI void _stdcall cml_LineTest (long int p_hDC, long int ulc_x,
long int ulc_y, long int lrc_x, long int lrc_y, long int nShift)
{
HDC hDC;
long int dx, dy;
long int k;
hDC = (HDC) p_hDC;
dx = labs(lrc_x - ulc_x);
dy = labs(lrc_y - ulc_y);
for (k=0; k < dx; k += nShift)
{
MoveToEx(hDC, ulc_x + k, ulc_y, NULL);
LineTo(hDC, lrc_x - k, lrc_y);
}
for (k=0; k < dy; k += nShift)
{
MoveToEx(hDC, ulc_x, ulc_y + k, NULL);
LineTo(hDC, lrc_x, lrc_y - k);
}
return;
Quote:
}
//.================================================================
// Coordinates are Pixel coord, not Twips!
// Draws a string in the center of window, at different angles.
DLLAPI void _stdcall cml_FontTest(long p_hdc, long cntr_x, long
cntr_y, int angle)
{
HDC hDC;
HFONT hfnt, hfntPrev;
LPSTR lpszRotate = "Chicks dig it.";
hDC = (HDC) p_hdc;
// Allocate memory for LOGFONT structure
PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));
// Specify font typeface name and weight
lstrcpy(plf->lfFaceName,"Courier New");
plf->lfWeight = FW_NORMAL;
// Set background mode to transparent for the text output
operation
// SetBkMode(hDC, TRANSPARENT);
for(angle=0;angle < 3600; angle += 450)
{
plf->lfEscapement = angle;
hfnt = CreateFontIndirect(plf);
hfntPrev = SelectObject(hDC, hfnt);
TextOut(hDC, cntr_x, cntr_y, lpszRotate,
lstrlen(lpszRotate));
SelectObject(hDC, hfntPrev);
DeleteObject(hfnt);
}
// SetBkMode(hDC, OPAQUE);
LocalFree((LOCALHANDLE) plf);
Quote:
}
In SOMEFILE.BAS:
Public Declare Function cml_UCase Lib "QMAP32.DLL" (ByVal str As
String) As String
Public Declare Function cml_OnlyDigits Lib "QMAP32.DLL" (ByVal str As
String) As Integer
Public Declare Function cml_strcmp Lib "QMAP32.DLL" (ByVal str0 As
String, ByVal str1 As String) As Integer
Public Declare Function cml_sizeof Lib "QMAP32.DLL" (ByVal cType As
Integer) As Long
Public Declare Sub cml_clipboundary Lib "QMAP32.DLL" _
(ByVal x_long As Long, ByVal x_lat As Long, ByVal y_long As Long,
ByVal y_lat As Long)
Public Declare Function cml_clipsegment Lib "QMAP32.DLL" (x_long As
Long, x_lat As Long, y_long As Long, y_lat As Long) As Integer
Public Declare Sub cml_clipShadow Lib "QMAP32.DLL" ()
Public Declare Sub cml_clipFromShadow Lib "QMAP32.DLL" ()
Public Declare Function cml_dsegs Lib "QMAP32.DLL" (SegData As Long)
As Long
Public Declare Function cml_dist2seg Lib "QMAP32.DLL" _
(ByVal px As Long, ByVal py As Long, ByVal s_x1 As Long, ByVal
s_y1 As Long, ByVal s_x2 As Long, ByVal s_y2 As Long) As Double
Public Declare Sub cml_LineTest Lib "QMAP32.DLL" _
(ByVal hDC As Long, ByVal ulc_x As Long, ByVal ulc_y As Long,
ByVal lrc_x As Long, ByVal lrc_y As Long, ByVal nStep As Long)
Public Declare Sub cml_FontTest Lib "QMAP32.DLL" _
(ByVal hDC As Long, ByVal cntr_x As Long, ByVal cntr_y As Long,
ByVal angle As Integer)