Solved

Followup DLL Question for all Wizards and Nietod

Posted on 1998-02-11
10
266 Views
Last Modified: 2013-12-03
To Nietod and all other Wizards:

I appreciate the assistance so far.
In working with the example given to me by nietod, I compiled the DLL example (with certain words that had to be capitalized) under MS VC++(Non IDE environment) and tried to test it under MS VB-V4.0 with no such luck.  What am I doing wrong?  Can you first test out the answer/solution to insure the solution is correct?   As for the number of points, you be the honest judge of that.

  Here is the example given and my make file, and what my environment is - that I use.

 I hope this helps.

 Midnightexpress
 Email: methorpe@burgoyne.com

 Added NOTE:  I use the Microsoft Maintainance programming Utility, I believe it's called,  rather than the IDE environment - because it is easier to use.  Can you suggest any suggestions on how the MS VC++ IDE could and can be better.  I have a somewhat of the idea on how to set up the DLL environmental workspace within the VC++ IDE.  What I am trying or rather attempting to do, is to make a DLL like Microsoft has done and make the DLL very generic in nature(like the examples used under MS VB), which can be used under any Windows application platforms.  Which can pass data( character, and numerical(by reference or by value)) to and from DLL "C" routines.

 In going to Expert-Exchange for help, I was referred to you by a programmer, because he has had great success in getting answers to their questions.  "No such Luck" means, VB returns an 'illegal error message' as a result of calling and/or returning from the DLL routine, or when trying to correct the error, I get a error saying routine not found - Even though the declarations in the DLL file and the declarations in MS VB match exactly.  Also, with running under different windows platforms, what are the universal data types.  From talking with people, who only drop hints - they say the data types in MS VB do not correctly match up.
Any suggestions?  It seems like I can get into the DLL routine, but when exiting from the routine - there are problems that happens that I have no clue of what is going on.   These errors, could be from a range of problems. Such as: 1) Improper Function declaration.
    2) Improper Function parameter declaration, with regards          to correct data type declaration.
    3) And maybe mismatch datatypes and
        returning/exiting from a function/procedure.
 
   Test the example out.

  Midnightexpress,  thanks.


 Can you help??
------------
** Here is the example that was given.

// #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#define DllImp __declspec(dllimport)
#define DllExp __declspec(dllexport)

 DllExp int Increment(int i);

BOOL WINAPI DllMain(HINSTANCE DLLHnd, ULONG Rsn, LPVOID Rsv) {
    switch (Rsn) {
       case DLL_PROCESS_ATTACH:
         break;
       case DLL_PROCESS_DETACH:
         break;
       case DLL_THREAD_ATTACH:
         break;
       case DLL_THREAD_DETACH:
         break;
    }
    return TRUE; // always return true!
 }

 DllExp int Increment(int i) {
    return i + 1;
 }
----------------------
#----------------------
# try1.MAK make file   <---- the make file.
#----------------------

#-- the try1 program set
try1.dll : try1.obj
     $(LINKER) $(DLLFLAGS) -OUT:try1.dll try1.obj $(GUILIBS) /map:dllout.map

try1.obj : try1.c
     $(CC) $(CFLAGSMT) try1.c   | more

-------------MY Environment for MS VC++
@echo off
rem
rem Root of Visual C++ installed files.
rem     Note: This batch file will not work on Windows NT if MSDevDir contains spaces.
rem
set MSDevDir=C:\MSDEV

rem
rem Root of Visual C++ files on cd-rom.
rem Remove "set vcsource=" if you don't want include cdrom in Visual C++ environment.
rem
set vcsource=E:\MSDEV

rem
rem VcOsDir is used to help create either a Windows 95 or Windows NT specific path.
rem
set VcOsDir=WIN95
if "%OS%" == "Windows_NT" set VcOsDir=WINNT

if "%1" == "x86" goto x86
if "%1" == "m68k" goto m68k
if "%1" == "mppc" goto mppc

echo Usage: vcvars32 [target]
echo     Where target is one of the following: x86, m68k, mppc.
if "%1" == "" goto default
goto done

:default
echo No target specified.  Selecting x86 as default target.

:x86
rem
echo Setting environment for building x86 target
rem
if "%vcsource%" == "" goto x86main
rem
rem Include cdrom files in environment.
rem
if "%OS%" == "Windows_NT" set PATH=%vcsource%\BIN;%vcsource%\BIN\%VcOsDir%;%PATH%
if "%OS%" == "" set PATH="%vcsource%\BIN";"%vcsource%\BIN\%VcOsDir%";"%PATH%"
set INCLUDE=%vcsource%\INCLUDE;%vcsource%\MFC\INCLUDE;%INCLUDE%
set LIB=%vcsource%\LIB;%vcsource%\MFC\LIB;%LIB%
set vcsource=

:x86main
if "%OS%" == "Windows_NT" set PATH=%MSDevDir%\BIN;%MSDevDir%\BIN\%VcOsDir%;%PATH%
if "%OS%" == "" set PATH="%MSDevDir%\BIN";"%MSDevDir%\BIN\%VcOsDir%";"%PATH%"
set INCLUDE=%MSDevDir%\INCLUDE;%MSDevDir%\MFC\INCLUDE;%INCLUDE%
set LIB=%MSDevDir%\LIB;%MSDevDir%\MFC\LIB;%LIB%
goto done

:m68k
rem
echo Setting environment for building m68k target
rem
if "%OS%" == "Windows_NT" set PATH=%MSDevDir%\mac\m68k\bin;%MSDevDir%\mac\bin;%MSDevDir%\BIN;%MSDevDir%\BIN\%VcOsDir%;%PATH%
if "%OS%" == "" set PATH="%MSDevDir%\mac\m68k\bin";"%MSDevDir%\mac\bin";"%MSDevDir%\BIN";"%MSDevDir%\BIN\%VcOsDir%";"%PATH%"
set INCLUDE=%MSDevDir%\mac\include;%MSDevDir%\mac\include\macos;%MSDevDir%\mac\include\mrc;%MSDevDir%\INCLUDE;%MSDevDir%\MFC\INCLUDE;%INCLUDE%
set LIB=%MSDevDir%\mac\m68k\lib;%MSDevDir%\LIB;%MSDevDir%\MFC\LIB;%LIB%
goto done

:mppc
rem
echo Setting environment for building mppc target
rem
if "%OS%" == "Windows_NT" set PATH=%MSDevDir%\mac\mppc\bin;%MSDevDir%\mac\bin;%MSDevDir%\BIN;%MSDevDir%\BIN\%VcOsDir%;%PATH%
if "%OS%" == "" set PATH="%MSDevDir%\mac\mppc\bin";"%MSDevDir%\mac\bin";"%MSDevDir%\BIN";"%MSDevDir%\BIN\%VcOsDir%";"%PATH%"
set INCLUDE=%MSDevDir%\mac\include;%MSDevDir%\mac\include\macos;%MSDevDir%\mac\include\sys;%MSDevDir%\mac\include\mrc;%MSDevDir%\INCLUDE;%MSDevDir%\MFC\INCLUDE;%INCLUDE%
set LIB=%MSDevDir%\mac\mppc\lib;%MSDevDir%\LIB;%MSDevDir%\MFC\LIB;%LIB%

:done
set VcOsDir=
-------
REM -------------------------------------------------------------
REM  MSC.BAT -- Set up environment for Microsoft C/C++ 7.0 NMAKE
REM -------------------------------------------------------------
SET CC=cl
SET CFLAGS=-c -DSTRICT -G3 -Ow -W3 -Zp -Tp
SET CFLAGSMT=-c -DSTRICT -G3 -MT -Ow -W3 -Zp -Tp
SET LINKER=link
SET GUIFLAGS=-SUBSYSTEM:windows
SET DLLFLAGS=-SUBSYSTEM:windows -DLL
SET GUILIBS=-DEFAULTLIB:user32.lib gdi32.lib winmm.lib comdlg32.lib comctl32.lib
SET RC=rc
SET RCVARS=-r -DWIN32
****************************************************
0
Comment
Question by:midnightexpress
  • 7
  • 3
10 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1411165
You said you had "no luck", but could you be more specific.  What is the problem?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1411166
Is there a reason you are using make files?  Using the VC deveopment environment is much easier.
0
 

Author Comment

by:midnightexpress
ID: 1411167
Edited text of question
0
 

Author Comment

by:midnightexpress
ID: 1411168
Edited text of question
0
 

Author Comment

by:midnightexpress
ID: 1411169
Edited text of question
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:nietod
ID: 1411170
I still don't see a discription of your problem.  

But lets try to get this working within VC's environment, rather than a make file.

You want a project that makes the Dll.
From the "file" menu go to "new..." option.
In the dialog box go to the "Projects" tab.
Choose "Win32 Dynamic link library" from the list.
Give the project a name like "TstDll"
Give it a path for you source and associated files, like "C:\TSTDLL"  (It will create this directory and some sub-directories of it.)
Make sure that "create new workspace" is selected.
Hit okay.

Then you'll have an empty workspace.
You need to add the source code file to the workspace.
Since it already exists, copy it into the directory used for the project.
Go to "Project" menu and select "add to Project"
Select "files" and choose the dll source code files.

Now you should be able to build it using the build option under the "build" menu.

Try this and let me know what's going on.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1411171
Allright, I didn't know (or remember) that basic was involved.
The first problem is name decoration.  C++ actually produced a function with a different name than what you specify.  This is to handle overloading.  Basic doesn't know about this so it is looking for the wrong name.
0
 
LVL 22

Accepted Solution

by:
nietod earned 0 total points
ID: 1411172
To fix this tell C++ not to decorate the name using the extern "C" directive like

DllExp extern "C" int Increment(int i) {
   return i + 1;
}

that will probably fix the problem where VB can't find the function.  

As for you other problem with "data not matching up", I can't help too much there.  The idea is that the VB side needs to declare parameters/return values that match the C++ side.  I don't know basic types, but in VC an "int" is 32 bits (4 bytes), a "short" is 16 bits (2 bytes), a "char" is one byte (no shock there).  They are all signed by default.  Althoug you don't really need to work about signed/unsigned too much.  

Also the parameters are passed in reversed order.  So if you declare

void F(int i,char ch)

The character is pushed first and then the integer.  make sure the basic side declares things so they are pushed in the order that C++ expects.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1411173
If you need more help, could you do me a favor and edit the question one more time and put some line breaks in.  I don't know why, but for some reason when I view your question it consists of very long lones that don't wrap.  I've never seen it do that before and it makes it very hard to read.  I may have missed important parts of you explanation because of it.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1411174
I didn't really solve your problems with my answer, but you did accept it.  Does that mean you've got it working?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

21 Experts available now in Live!

Get 1:1 Help Now