Method does not return a value..

Failed to get a value from a method using my dll in my c# app.

Page 1 of 1

11 Replies - 3799 Views - Last Post: 18 July 2010 - 07:26 PM Rate Topic: -----

#1 whitekeeper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-July 09

Method does not return a value..

Posted 07 July 2010 - 06:07 AM

Can anyone tell me how come my 'DisplayValue()' method does not return a value from the dll i have created in c? The 'DisplayValue()' method in my dll is a 'long' data type while the one in my c# app(shown below) uses 'int' because that does not give me an error. I have trouble trying to do marshalling for these data types.. Marshalling data types from c to c# guides from some of you would be good too or you can give me some advice. Thank you guys.

--------------------------------------------------------------------------------------------------------------------------
:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; 

namespace ForFun
{
    public partial class Form1 : Form
    {
        [DllImport("test.dll")]
        public static extern int DisplayValue();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtBox.Text = DisplayValue() + " ";
        }

    }
}

:
--------------------------------------------------------------------------------------------------------------------------

DLL

// read.cpp: implementation of the read class.
//
//////////////////////////////////////////////////////////////////////

#include "read.h"
#include <stdio.h>
#include "stdafx.h"
#include <tchar.h>
#include <windows.h>
#include "Driver.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

read::read()
{

}

read::~read()
{

}

extern "C"
{
	void ShowErrorMessage(LRESULT errorCode);
	void ShowDeviceName(ULONG deviceNumber);

  __declspec(dllexport) long DisplayValue()
  {
	//LPCSTR info =  "";

	LRESULT result = SUCCESS;
	
	int i;
	float value;
	
	// Device number for open.
	ULONG deviceNum = 0;		
	
	// Driver Handle opened.
	LONG driverHandle = 0;		
	
	// Channel count for scan data.
	USHORT numChannel = 4;
	//
	USHORT channel[] = {0, 1, 2, 3};
	USHORT expChannel[] = {0};
	USHORT boardID[] = {0};
	// Gain codes for scan data.
	USHORT gainCode[] = {4, 0, 1, 2};
	
	// Return data
	FLOAT data[64];

	// Voltage in expansion
	PT_MAIVoltageInExp ptMAIVoltageInExp = {0};

	DEVFEATURES devFeature = {0};
	PT_DeviceGetFeatures ptDevFeature = {0};
	
	DEVCONFIG_AI devConfig = {0};
	PT_AIGetConfig ptAIGetConfig = {0};

	do 
	{
		// Open device
		result = DRV_DeviceOpen(deviceNum, &driverHandle);
		if (result != SUCCESS)
		{
			break;
		}	

		// Show device name
		ShowDeviceName(deviceNum);		

		// Get device config
		ptAIGetConfig.buffer = &devConfig;
		ptAIGetConfig.size = sizeof(DEVCONFIG_AI);
		result = DRV_AIGetConfig(driverHandle, &ptAIGetConfig);
		if (result != SUCCESS)
		{
			break;
		}

		// Get data
		ptMAIVoltageInExp.NumChan = numChannel;
		ptMAIVoltageInExp.DasChanArray = channel;
		ptMAIVoltageInExp.DasGainArray = gainCode;
		ptMAIVoltageInExp.ExpChanArray = expChannel;
		ptMAIVoltageInExp.VoltageArray = data;			
		result = DRV_MAIVoltageInExp(driverHandle, &ptMAIVoltageInExp);
		if (result != SUCCESS)
		{
			_tprintf(TEXT("Result not success."));
			break;
		}

		// Output data
		for (i = 0; i < numChannel; i++)
		{
			_tprintf(TEXT("i) %f "), data[i]);
			value = data[i];
			//info = "hihi";
		}
		_tprintf(TEXT("\n\n"));

	} while (FALSE);

	// If error occur, print error message
	if (result != SUCCESS)
	{   
		_tprintf(TEXT("Result not success.#2"));
		ShowErrorMessage(result);
	    // _tprintf(TEXT("Result not success.#2-end"));
	}

	// Close device
	if (driverHandle)
	{
		DRV_DeviceClose(&driverHandle);
	}

	_tprintf(TEXT("Press any key to exit this example...\n"));
	_gettchar();

    return value;
	//return 0;
  }

  //------------------------------------------------------------------------------
// DESCRIPTION: Show error message.
// RETURN VALUES:
// -- none.
// PARAMETER:
// -- [in] errorCode          Error code.
//------------------------------------------------------------------------------
void ShowErrorMessage(LRESULT errorCode)
{
	TCHAR szErrorMessage[256];
	DRV_GetErrorMessage(errorCode, szErrorMessage);
	//MessageBox(NULL, szErrorMessage, NULL, MB_OK);
	_tprintf(TEXT("%d, %s\n"), errorCode, szErrorMessage);	
	
}
//------------------------------------------------------------------------------
// DESCRIPTION: Show device name.
// RETURN VALUES:
// -- none.
// PARAMETER:
// -- [in] deviceNumber         Device number.
//------------------------------------------------------------------------------
void ShowDeviceName(ULONG deviceNumber)
{
	LRESULT result = SUCCESS;
	
	SHORT deviceCount;	
	LPDEVLIST lpDeviceList = NULL;

	int i;

	// Get device count
	result = DRV_DeviceGetNumOfList(&deviceCount);
	if (result != SUCCESS)
	{
		return;
	}

	// Get device information list
	lpDeviceList = (LPDEVLIST) calloc(deviceCount, sizeof(DEVLIST)); 
	result = DRV_DeviceGetList(lpDeviceList, deviceCount, &deviceCount);
	if (result != SUCCESS)
	{
		free(lpDeviceList);
		return;
	}

	// Print device name
	for (i = 0; i < deviceCount; i++)
	{
		if (lpDeviceList[i].dwDeviceNum == deviceNumber)
		{
			_tprintf(TEXT("[Device Name]%s\n"), lpDeviceList[i].szDeviceName);
			break;
		}
	}

	// Free memory
	free(lpDeviceList);
}

}





Any comments? I'll see if i have time tomorrow to paste the code as i don't have it now but i'm trying best to explain here. =) But do you have any clue?

This post has been edited by whitekeeper: 07 July 2010 - 09:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Method does not return a value..

#2 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Method does not return a value..

Posted 07 July 2010 - 06:22 AM

first off ::code::

secondly if we can't see the DisplayValue method how can we say why it doesn't return a value?
Was This Post Helpful? 0
  • +
  • -

#3 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Method does not return a value..

Posted 07 July 2010 - 07:28 AM

View PostAdkins, on 07 July 2010 - 05:22 AM, said:

if we can't see the DisplayValue method how can we say why it doesn't return a value?

Was This Post Helpful? 1
  • +
  • -

#4 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Method does not return a value..

Posted 07 July 2010 - 07:34 AM

Also try:

int ret = DisplayValue();
txtBox.Text = ret.ToString() + " ";

Was This Post Helpful? 0
  • +
  • -

#5 whitekeeper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-July 09

Re: Method does not return a value..

Posted 08 July 2010 - 05:30 AM

Dll already uploaded just now...any ideas?
Was This Post Helpful? 0
  • +
  • -

#6 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Method does not return a value..

Posted 08 July 2010 - 06:46 AM

Well if it's the DLL that needs debugging then I don't think this is the right forum in particular, but one thing you should try is running the C# program, putting a break point in and checking to see if the method returns anything at all by setting up a watch variable (bottom left hand corner the window usually is).
Was This Post Helpful? 0
  • +
  • -

#7 Ikonmx   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 19-August 09

Re: Method does not return a value..

Posted 08 July 2010 - 10:17 AM

You should try if the dll can find the device. Try calling the method ShowDeviceName. Because your are limiting the device to be number 0.
Was This Post Helpful? 0
  • +
  • -

#8 whitekeeper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-July 09

Re: Method does not return a value..

Posted 08 July 2010 - 06:21 PM

showdevicename() can work. I tried the dll with my WINCE console application and it works but it doesn't work with WINCE device application.
Was This Post Helpful? 0
  • +
  • -

#9 Ikonmx   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 19-August 09

Re: Method does not return a value..

Posted 10 July 2010 - 12:58 AM

I guess I found it.. in the implementation of your dll, right before you return the value you wait for a console input

126
    _tprintf(TEXT("Press any key to exit this example...\n"));
127
    _gettchar();



Hope it helps.
Was This Post Helpful? 0
  • +
  • -

#10 whitekeeper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-July 09

Re: Method does not return a value..

Posted 11 July 2010 - 07:21 PM

it returns some value like "10" and if i change the "DisplayValue" data type to "double", i get "568289" as a value. The value i'm suppose to get is "0.67" or so as its from a voltage.
Was This Post Helpful? 0
  • +
  • -

#11 Ikonmx   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 19-August 09

Re: Method does not return a value..

Posted 13 July 2010 - 04:36 PM

You should ask you device manufacturer, how to process the input.

101
        // Output data
102
        for (i = 0; i < numChannel; i++)
103
        {
104
            _tprintf(TEXT("i) %f "), data[i]);
105
            value = data[i];
106
            //info = "hihi";
107
        }



In this loop you just assign the data[i] to your value variable.

At the end you have data[numChannel-1] in your value variable. If that what you really need to do?

Could you please put one output from the driver runned from console.
Was This Post Helpful? 0
  • +
  • -

#12 whitekeeper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-July 09

Re: Method does not return a value..

Posted 18 July 2010 - 07:26 PM

Thank you everyone for helping me but i think i might have found the solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1