• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Followup DLL Question for all Wizards and Nietod

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.

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

    switch (Rsn) {
       case DLL_THREAD_ATTACH:
       case DLL_THREAD_DETACH:
    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 Root of Visual C++ installed files.
rem     Note: This batch file will not work on Windows NT if MSDevDir contains spaces.
set MSDevDir=C:\MSDEV

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

rem VcOsDir is used to help create either a Windows 95 or Windows NT specific path.
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

echo No target specified.  Selecting x86 as default target.

echo Setting environment for building x86 target
if "%vcsource%" == "" goto x86main
rem Include cdrom files in environment.
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=

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

echo Setting environment for building m68k target
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

echo Setting environment for building mppc target
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%

set VcOsDir=
REM -------------------------------------------------------------
REM  MSC.BAT -- Set up environment for Microsoft C/C++ 7.0 NMAKE
REM -------------------------------------------------------------
SET CFLAGS=-c -DSTRICT -G3 -Ow -W3 -Zp -Tp
SET GUILIBS=-DEFAULTLIB:user32.lib gdi32.lib winmm.lib comdlg32.lib comctl32.lib
  • 7
  • 3
1 Solution
You said you had "no luck", but could you be more specific.  What is the problem?
Is there a reason you are using make files?  Using the VC deveopment environment is much easier.
midnightexpressAuthor Commented:
Edited text of question
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

midnightexpressAuthor Commented:
Edited text of question
midnightexpressAuthor Commented:
Edited text of question
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.

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

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.  
I didn't really solve your problems with my answer, but you did accept it.  Does that mean you've got it working?

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now