LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Simple DLL problem

I'm dabbling with DLL writing and can't figure out how to pass a string to a
DLL. As far as I can see, it should be simple.

My DLL function, in the DLL "test.dll", is simply

TEST_API int test(LPCTSTR WinName)
{
return 0;
}

Instead of LPCTSTR I've also tried the more conventional "char *WinName",
and a few others. All give the same error; "The exception Priviledged
Instruction occurred in the application at location (blah)." The error
message doesn't seem too specific; exactly the same thing happens if I
deliberately copy some text to an uninitialised pointer.

I'm calling the function using a "Call Library Node", passing a
pre-generated string in the form of a C string pointer. The calling
convention is set to "winapi" but doesn't seem too relevant.


If I remove the string argument and change the function definition to take
(void) then it works, in that I can put integers in the return code and pass
them back to Labview, so it's the passing of the string that's the problem.
However, if I have more code in there and I use the Visual C++ debugger, the
error message is only generated on hitting the "return" line, and I can see
the string that has been passed in when I look at "WinName" in the debugger.

Can anyone shed some light on this?

--
Craig Graham
Physicist/Labview Programmer
Lancaster University, UK
0 Kudos
Message 1 of 4
(2,564 Views)
Its usually easiest to pass a string as a C string pointer (CStr). Your DLL function can just use a regular pointer to a string (character array). I'll attach an example program that should help.
0 Kudos
Message 2 of 4
(2,564 Views)
Thanks; serves both as a useful reference example and to show that I'm doing
nothing stupid. I don't understand why my version doesn't work; in both mine
and yours, the passed variable is defined as a "C String Pointer" at the
Labview side and as a char* at the DLL side.

I created the DLL by using the "new project wizard" in Visual C++, and
there's a lot of extra cack in there that's added by the wizard; it's
possible this is what's causing the problem. I'll have another crack when I
get some more time.

Aaron Marks wrote in message
news:50650000000500000026490000-1003545641000@exchange.ni.com...
> Its usually easiest to pass a string as a C string pointer (CStr).
> Your DLL function can just use a regular pointer to a string
> (character
array). I'll attach an example program that should help.
0 Kudos
Message 3 of 4
(2,564 Views)
Craig;

I am going to throw some random thoughts that may help you in your situation:

- I avoid to use VC's wizard. It gives (and gave me) a lot of headaches. All it does it to add a lot of garbage to your project that nobody understand.

- If your source file is a .cpp, wrap your function prototypes inside the extern "C" declaration.

- Always initialize every variable in LabVIEW side. For example, for your function you should wire an empty string in the input side of the "Call Library Node" for the "WinName" variable.

- I claim ignorance in this one, but seems to simplify my projects. Exclude rarely-used Windows header files by adding the following statement before your headers "include":

#define VC_EXTRALEAN

I hope this can be of help. If I co
me up with some more suggestion, I'll let you know.

Best regards;
Enrique
www.vartortech.com
0 Kudos
Message 4 of 4
(2,564 Views)