By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

Call a C function that has parameter of float got NotSupportedException?

P: n/a
Dear All:

I create a very simple DLL by using EVC to test the problem. (The platform I
am working for those program is WinCE.NET)

************************************************** *****
The header looks like:

#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

TESTDLL_API LONG fnTestF(FLOAT var1);

************************************************** *****
The C source looks like:
#include "stdafx.h"
#include "TestDll.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TESTDLL_API LONG fnTestF(FLOAT var1)
{
return 0;
}

************************************************** *****
The DEF file look like:
LIBRARY TestDll
EXPORTS
fnTestF
************************************************** *****

After build up this DLL, I try to write a C# program to call this function.
The C# program looks like:

[DllImport("testdll.dll")]
private static extern Int32 fnTestF
(
Single fPeriod
);

When I call this function likes bellow:
Single fVal = 0.1f;
Int32 i32Ret;

try
{
i32Ret = fnTestF(fVal);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception!");
}

I always get an exception called "NotSupportedException".

Any idea about this?

Thanks!

Tony Liu

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tony, if you're code is in a .CPP file, you may be generating a mangled name
function. Open your DLL in in the "Depends" utility and look in the
exports section to see what the name look likes. If it is mangled, just add
the following to your .H file.

#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

// ---- new code ----
#ifdef __cplusplus
extern "C"{
#endif
// ----------------------

TESTDLL_API LONG fnTestF(FLOAT var1);

// ---- new code ----
#ifdef __cplusplus
};
#endif
// ----------------------
Gary ...
"Tony Liu" <he******@ms21.hinet.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Dear All:

I create a very simple DLL by using EVC to test the problem. (The platform I am working for those program is WinCE.NET)

************************************************** *****
The header looks like:

#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

TESTDLL_API LONG fnTestF(FLOAT var1);

************************************************** *****
The C source looks like:
#include "stdafx.h"
#include "TestDll.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TESTDLL_API LONG fnTestF(FLOAT var1)
{
return 0;
}

************************************************** *****
The DEF file look like:
LIBRARY TestDll
EXPORTS
fnTestF
************************************************** *****

After build up this DLL, I try to write a C# program to call this function. The C# program looks like:

[DllImport("testdll.dll")]
private static extern Int32 fnTestF
(
Single fPeriod
);

When I call this function likes bellow:
Single fVal = 0.1f;
Int32 i32Ret;

try
{
i32Ret = fnTestF(fVal);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception!");
}

I always get an exception called "NotSupportedException".

Any idea about this?

Thanks!

Tony Liu


Nov 16 '05 #2

P: n/a
Finally, I found a document in MSDN 2003:
The topic of the document is "Non Blittable Types and Marshaling Support"
A section of this document:
Visual C and Visual C++ Marshaling Results
The following table lists Visual C and Visual C++ types used by the
marshaler for managed types, with Windows CE .NET as the operating system
hosting the unmanaged code. This list is not comprehensive.

Managed Code
Visual C# Types
Managed Code
Visual Basic Types
Unmanaged Code
ByVal
Unmanaged Code
ByRef

bool
Boolean
BYTE
BYTE *

int
Integer
INT32
INT32 *

short
Short
SHORT
SHORT *

long
Long
Not supported
INT64 *

char
Char
WCHAR
WCHAR *

float
Single
Not supported
FLOAT *

double
Double
Not supported
DOUBLE *

String
String
WCHAR *
Not supported

StringBuilder
StringBuilder
WCHAR *
Not supported

DateTime
DateTime
Not supported
Not supported

int[]
Integer()
INT32 *, INT32[]
Not supported

I think that is the reason why I cannot pass a "FLOAT" by value.

Tony

"Gary James" <ga***@iotech.com> ???
news:e6*************@tk2msftngp13.phx.gbl ???...
Tony, if you're code is in a .CPP file, you may be generating a mangled name function. Open your DLL in in the "Depends" utility and look in the
exports section to see what the name look likes. If it is mangled, just add the following to your .H file.

#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

// ---- new code ----
#ifdef __cplusplus
extern "C"{
#endif
// ----------------------

TESTDLL_API LONG fnTestF(FLOAT var1);

// ---- new code ----
#ifdef __cplusplus
};
#endif
// ----------------------
Gary ...
"Tony Liu" <he******@ms21.hinet.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Dear All:

I create a very simple DLL by using EVC to test the problem. (The
platform I
am working for those program is WinCE.NET)

************************************************** *****
The header looks like:

#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

TESTDLL_API LONG fnTestF(FLOAT var1);

************************************************** *****
The C source looks like:
#include "stdafx.h"
#include "TestDll.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TESTDLL_API LONG fnTestF(FLOAT var1)
{
return 0;
}

************************************************** *****
The DEF file look like:
LIBRARY TestDll
EXPORTS
fnTestF
************************************************** *****

After build up this DLL, I try to write a C# program to call this

function.
The C# program looks like:

[DllImport("testdll.dll")]
private static extern Int32 fnTestF
(
Single fPeriod
);

When I call this function likes bellow:
Single fVal = 0.1f;
Int32 i32Ret;

try
{
i32Ret = fnTestF(fVal);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception!");
}

I always get an exception called "NotSupportedException".

Any idea about this?

Thanks!

Tony Liu

Nov 16 '05 #3

P: n/a
Finally, I found a document in MSDN 2003:
The topic of the document is "Non Blittable Types and Marshaling Support"
A section of this document:
Visual C and Visual C++ Marshaling Results
The following table lists Visual C and Visual C++ types used by the
marshaler for managed types, with Windows CE .NET as the operating system
hosting the unmanaged code. This list is not comprehensive.

Managed Code
Visual C# Types
Managed Code
Visual Basic Types
Unmanaged Code
ByVal
Unmanaged Code
ByRef

bool
Boolean
BYTE
BYTE *

int
Integer
INT32
INT32 *

short
Short
SHORT
SHORT *

long
Long
Not supported
INT64 *

char
Char
WCHAR
WCHAR *

float
Single
Not supported
FLOAT *

double
Double
Not supported
DOUBLE *

String
String
WCHAR *
Not supported

StringBuilder
StringBuilder
WCHAR *
Not supported

DateTime
DateTime
Not supported
Not supported

int[]
Integer()
INT32 *, INT32[]
Not supported

I think that is the reason why I cannot pass a "FLOAT" by value.

Tony

"Gary James" <ga***@iotech.com> ???
news:e6*************@tk2msftngp13.phx.gbl ???...
Tony, if you're code is in a .CPP file, you may be generating a mangled name function. Open your DLL in in the "Depends" utility and look in the
exports section to see what the name look likes. If it is mangled, just add the following to your .H file.

#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

// ---- new code ----
#ifdef __cplusplus
extern "C"{
#endif
// ----------------------

TESTDLL_API LONG fnTestF(FLOAT var1);

// ---- new code ----
#ifdef __cplusplus
};
#endif
// ----------------------
Gary ...
"Tony Liu" <he******@ms21.hinet.net> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Dear All:

I create a very simple DLL by using EVC to test the problem. (The
platform I
am working for those program is WinCE.NET)

************************************************** *****
The header looks like:

#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

TESTDLL_API LONG fnTestF(FLOAT var1);

************************************************** *****
The C source looks like:
#include "stdafx.h"
#include "TestDll.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TESTDLL_API LONG fnTestF(FLOAT var1)
{
return 0;
}

************************************************** *****
The DEF file look like:
LIBRARY TestDll
EXPORTS
fnTestF
************************************************** *****

After build up this DLL, I try to write a C# program to call this

function.
The C# program looks like:

[DllImport("testdll.dll")]
private static extern Int32 fnTestF
(
Single fPeriod
);

When I call this function likes bellow:
Single fVal = 0.1f;
Int32 i32Ret;

try
{
i32Ret = fnTestF(fVal);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception!");
}

I always get an exception called "NotSupportedException".

Any idea about this?

Thanks!

Tony Liu

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.