Jump to content

Custom C++ DLL data type support in LV


Recommended Posts

Hello All,

I'm looking to use some c++ code in my labview application using the call function library function. I have a class in C++ which is a custom data type. I need to send this variable into my c++ functions. I want to allocate memory for it inside one of my dll functions and pass a pointer to it back to labview. I want to use this pointer to pass through to all my subsequent functions requiring the pointer. I have been looking online for support for custom data types in labview dlls and cannot seem to find any acceptable solutions.

I have attached the C++ code for my data type below. Any help will be greatly appreciated.

#include "wmsdk.h"

#include "wmsysprf.h"

#include "mil.h"

#include "stdio.h"

#include "conio.h"

#include <assert.h>

class CMPEG4Streaming

{

public:

CMPEG4Streaming();

~CMPEG4Streaming();

bool SetStreamingParameters( int PortNum, const WCHAR * ArchiveFile, int VideoSizeX, int VideoSizeY, int AvgTimePerFrame, int RateControl);

inline int GetStreamingVideoSizeX(){ return m_VideoSizeX;}

inline int GetStreamingVideoSizeY(){ return m_VideoSizeY;}

bool StreamSample( void *DataPtr, int NumBytes, bool IsIFrame, unsigned long long cnsSampleTime);

private:

//We have 1 profile manager, and then 1 profile PER Stream

static IWMProfileManager *m_pProfileMgr;

static int CMPEG4StreamingInstanceCount;

IWMProfile *m_pProfile;

IWMWriter *m_pWriter;

IWMWriterAdvanced *m_pWriterAdvanced3;

IWMWriterNetworkSink *m_pWriterNetworkSink;

IWMWriterFileSink *m_pWriterFileSink;

int m_FirstIFrame;

DWORD m_PortNum;

const WCHAR * m_ArchiveFile;

int m_VideoSizeX;

int m_VideoSizeY;

//int m_VideoFormat;

//int m_VideoResolution;

void PrintErrorMessage( char *, HRESULT);

HRESULT CreateProfile( int PortNum, const WCHAR * ArchiveFile, int VideoSizeX, int VideoSizeY, int AvgTimePerFrame, int RateControl);

HRESULT ModifyProfile( int PortNum, const WCHAR * ArchiveFile, int VideoSizeX, int VideoSizeY, int AvgTimePerFrame, int RateControl);

HRESULT FillConfig ( IWMStreamConfig *pConfig, int VideoSizeX, int VideoSizeY, int AvgTimePerFrame, int RateControl);

HRESULT SetSinks ( unsigned long PortNum, const WCHAR * ArchiveFile);

};

Basically I need to be able to get a pointer to CMPEG4Streaming inside labview by allocating memory for it in my c++ dll.

My dll function would be something like this:

void allocateMPEG4 (CMPEG4Streaming *testvariable)

{

testvariable = new CMPEG4Streaming();

}

I would then pass the pointer returned in labview to all my subsequent functions in the dll.

Thanks!

Link to comment

QUOTE(rahimp @ Mar 20 2007, 03:48 PM)

I want to allocate memory for it inside one of my dll functions and pass a pointer to it back to labview. I want to use this pointer to pass through to all my subsequent functions requiring the pointer.

If you're only using a pointer (i.e., you don't need to actually access the values in G) then tell LabVIEW it's an int32 (by pointer). Then you can just pass that value around into your various other DLL calls without LabVIEW ever having to care what type it is.

If you need to actually interpret that data in LabVIEW then it's a little trickier. You have to create a cluster with the same layout as your C++ data structure. It looks like you have just a bunch of pointers and ints, so fortunately you're spared alignment issues on Windows (LabVIEW doesn't use native alignment on Windows). You can just create a cluster with an int32 for each of the elements in that class and pass it in as a void*. As long as you don't need to dereference any of the pointers inside the class you're fine. That includes the strings. You can't get to those unless you write a c function to dereference the pointer and write into your own string.

By far the easiest thing to do is just pass the pointer around without trying to get LabVIEW to interpret the data.

Link to comment

QUOTE(Adam Kemp @ Mar 20 2007, 05:56 PM)

If you're only using a pointer (i.e., you don't need to actually access the values in G) then tell LabVIEW it's an int32 (by pointer). Then you can just pass that value around into your various other DLL calls without LabVIEW ever having to care what type it is.

If you need to actually interpret that data in LabVIEW then it's a little trickier.

I dont need to interpret the data in LabVIEW. I just need to declare a pointer to the data in LabVIEW and then allocate memory for it in c++ and then pass it back into LabVIEW. From there I will be passing the pointer back to the subsequent c++ dll calls.

What I'm still confused about is what I would initially pass into the dll. For example if my function is as follows:

void allocateMPEG4 (CMPEG4Streaming *testvariable)

{

testvariable = new CMPEG4Streaming();

}

Then I would initially need to send pointer testvariable into the function from LabVIEW. Would I simply send in an unsigned 32 bit int? What sort of data type should I be sending into the call library function node? As well on the receiving side of the call library function would I just set the following:

Parameter: arg1

Type: numeric

Data Type: Unsigned 32-bit Integer

Pass: Pointer to Value

Or would this configuration be better on the receiving end:

Parameter: arg1

Type: Adapt to Type

Data Format: Handles by Value

Ideally I'd want my function in C++ to be as follows:

CMPEG4Streaming *allocateMPEG4 (void)

{

CMPEG4Streaming *testvariable = new CMPEG4Streaming();

return testvariable;

}

Is it at all possible to use the funciton return so that I do not have to send anything in from LabVIEW?

Thanks,

Link to comment

Just tell LabVIEW that your function returns an int32. That's your pointer. A pointer is just a number, after all. On a 32-bit system that number is 32 bits, so you just need an int32. Then for any function which takes a pointer you can just tell LabVIEW that it takes an int32. LabVIEW doesn't ever need to know what type it really is.

Link to comment

Thanks for your help Adam. I tried what you suggested and things seem to be working out just fine!

I have to go through my c++ code and convert some things to pointers in order to complete the integration.

I have one question though, is it possible to send decimal LabVIEW strings into functions in my DLL which accept char *? Basically are LabVIEW strings compatible with C++ or do I have to manipulate the strings in order to put them into an acceptable format before sending to my DLL functions?

Thanks,

Rahim

Link to comment

QUOTE(rahimp @ Mar 21 2007, 11:18 AM)

Thanks for your help Adam. I tried what you suggested and things seem to be working out just fine!

I have to go through my c++ code and convert some things to pointers in order to complete the integration.

I have one question though, is it possible to send decimal LabVIEW strings into functions in my DLL which accept char *? Basically are LabVIEW strings compatible with C++ or do I have to manipulate the strings in order to put them into an acceptable format before sending to my DLL functions?

Thanks,

Rahim

When you configure your call library node you tell it that the argument is a string, and then you tell it what kind of string. C String Pointer is one choice (it will say "CStr" in the function prototype), and that is a char*.

If you're just sending a string to the DLL then that's all you need, but if you're also wanting the DLL to fill that string then you have to allocate the buffer first (in G). Usually people do this by initializing an array of U8s and then casting that to a string. In LabVIEW 8.2 you can actually configure the call to make sure that the string has a certain minimum size, so that extra code isn't necessary. Either way, you can then wire the output of that argument into LabVIEW as a normal string.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.