Why is a "user breakpoint" called when I run my project with imported .lib, not when code is inline?

557 views Asked by At

The Situation

I am writing a wrapper library for GPIB communications for setting up specific instruments according to a clients specifications to automate some of their processes. I have to use C++ and the old '98 compiler in VC++ 6.0 running on a Windows NT machine to maintain compatibility with some other devices they use regularly.

I am trying to make a class that combines some GPIB commands into easier to remember functions, while also keeping the capability of directly communicating with the instruments. To that end, I have compiled different parts of my project into different libs and dlls, each dll being a different device that they might want to communicate with. I also made a generic dll base class from which all the specific instrument classes inherit, hopefully making the whole setup as modular as possible.

The Problem

So, with all that said, I have an issue when I try to test the different dlls/modules. I created a project to test the generic base class, added the .lib file to the project, which links to the .dll, and also #included the header file for that dll. testGeneric.cpp looks like this:

#include "GENERIC.h"

void main(void) {
    GPIBInstrument hp(2); //connects to device at primary address #2
    hp.write("*IDN?");
}

Super simple. To be clear, I also have the GENERIC.lib linked in the "Resource Files" folder in VC++ 6.0, and I have GENERIC.dll accessible from the path variable.

Moving on, GENERIC.h looks like this (select parts):

#ifndef GENERIC_H
#define GENERIC_H

#include <string>
#include <windows.h>
#include "decl-32.h"

#ifdef GENERIC_EXPORT
  #define GENERIC_API __declspec(dllexport)
#else
  #define GENERIC_API __declspec(dllimport)
#endif

...(Inline exception classes)...

class GENERIC_API GPIBInstrument {
    ...
    public:
    void write(std::string command);
    ...
};

#endif

Just showing the relevant methods. Then GENERIC.cpp:

#define GENERIC_EXPORT
#include "GENERIC.h"
...

void GPIBInstrument::write(std::string command) {
    ibwrt (handle, &command[0u], command.length());
    std::cout << command << std::endl;
    if (ibsta & TIMO) {
      timeoutError();
    }
    if (ibsta & ERR) {
      error("Unable to write command to instrument: " + command);
    }
}

So, looks pretty good right? No issues. Compiles fine. I try running it, and BLAM! I get this: "User breakpoint called from code at 0x77f7645c". So, then I thought, well maybe it would work if I put all the code from GENERIC.h and GENERIC.cpp into one file, and #included that file all as inline code. So I tried it, and it and it compiled nicely, and ran fine.

Question (<-AHA!... But...)

What am I doing wrong!? Something with the way I'm making the .dll? Or the .lib? Or something else entirely?

EDIT (WHY!?)

So, after a bit of debugging, I found that it was something to do with passing a string literal. So I just modified it to:

std::string command = "*IDN?";
hp.write(command);

and it worked fine. My followup question, is why? What's the difference between having a string literal passed, versus assigning that to a variable and then passing it in?

1

There are 1 answers

2
ScottMcP-MVP On BEST ANSWER

Using complex types such as std::string as a parameter at a DLL boundary is tricky. You must ensure that the exe and the DLL use the exact same instance of the library code. This requires that you build them both to use the same version of the DLL version of the runtime library.