Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm having a problem when calling a function of a dll

I'm not a C++ developer so I don't know too much about this. I don't know why I'm having this problem or how to solve it. The problem is the following:

I have this method in my c++ dll:

extern "C" void _declspec(dllexport)  setSheetValues(float minx, float miny, float maxx,
        float maxy, int &ResultsNumber)
{
	//Sheet Parameters
	m_minX = minx;
	m_minY = miny;
	m_maxX = maxx;
	m_maxY = maxy;
	isSheetDefined = 1;
	ResultsNumber=654;
}


And when I try to call it from my VB.NET project I have the exception of the title.

I call it this way:

Private Declare Function setSheetValues Lib "MyDll.dll" Alias "setSheetValues" (ByVal minx As Double,
    ByVal miny As Double, ByVal maxx As Double,
    ByVal maxy As Double, ByRef ResultsNumber As Integer) As Integer

Dim rs As Integer = 4
setSheetValues(3, 3, 5, 5, rs)


I found that the problem is when it reach this operation in the dll:

ResultsNumber=654


What am I doing wrong?

What I want is to get the new result of ResultNumber back in my VB.NET project.

Of course this is a simple test but the problem is the same one.

Hope you could explain what is happening to me.

Thanks in advance,

Guido
Posted
Updated 4-Dec-09 2:58am
v3

I am surprised your Dll even compiles. There are no references in C, and all dll exports need to be C functions. Anyway, try replacing int &ResultsNumber with int *ResultsNumber and assign the value like this:

*ResultsNumber=654;
 
Share this answer
 
Hi,

First of all, thanks for the quick answer.

I tried what you comment, but continue throwing the exception.

If I take that line, all works well, but I can't find a way to get back a result.

Thanks,

Guido
 
Share this answer
 
[friendly tone]
You little beast are messing up with types!
[/friendly tone]

You're passing double values to a C function expecting float ones (this mess up the stack for the final parameter, since sizeof(double) != sizeof(float)).

Change the DLL function to:
extern "C"  void _declspec(dllexport)  setSheetValues(double minx, double miny, double maxx,
        double maxy, int *pResultsNumber)
{
	//Sheet Parameters
	m_minX = minx;
	m_minY = miny;
	m_maxX = maxx;
	m_maxY = maxy;
	isSheetDefined = 1;
	*pResultsNumber=654;
}


[edit]
With the proposed changes, the program runs fine on my system.
[/edit]

:)
 
Share this answer
 
v2
Hi,

Thanks for the answer!

I tried what you say, but even that sizeOf are diferent, this don't solve my problem :((

I can take off the float values and only let de ResultNumber and will crash anyways.

I see an example where there was a function:

void assign(int& to, int from) {
    to = from;  // Will change the actual parameter in the call.
}


but this won't work for me... I suppouse because I'm calling it from an extern program.

Sorry I can't be more specific, any help is really usefull!

Thanks,

Guido
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900