473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Translation from C to C#

Hi

First of all sorry for large post. I know its big, probably most won't
get to the end, but I'm running out of options.

I've searched web, I've found some demo project on www.codeproject.com
but there is only basic info about it. :-(

Could someone please translated it for me? I'm sitting on it for a few
days now and I can't get through :-(

First of all structures. I've translated most of them but I want to be sure:

<C>
/* TWON_ONEVALUE. Container for one value. */
typedef struct {
TW_UINT16 ItemType;
TW_UINT32 Item;
} TW_ONEVALUE, FAR * pTW_ONEVALUE;
/* DAT_CAPABILITY. Used by application to get/set capability from/in a
data source. */
typedef struct {
TW_UINT16 Cap; /* id of capability to set or get, e.g.
CAP_BRIGHTNESS */
TW_UINT16 ConType; /* TWON_ONEVALUE, _RANGE, _ENUMERATION or _ARRAY
*/
TW_HANDLE hContainer; /* Handle to container of type Dat
*/
} TW_CAPABILITY, FAR * pTW_CAPABILITY;

/* Fixed point structure type. */
typedef struct {
TW_INT16 Whole; /* maintains the sign */
TW_UINT16 Frac;
} TW_FIX32, FAR *pTW_FIX32;
</C>

And now the code that these structs are use in:

<C>
TW_UINT16 HandleSetCap(pCAP_STATESTORAGEITEM pCapData, pTW_CAPABILITY
pCap, TW_UINT16 *pStatus)
{
int i=0;
TW_UINT16 twRc = TWRC_FAILURE;
*pStatus = TWCC_BADVALUE;

pTW_ONEVALUE pOne=(pTW_ONEVALUE)GlobalLock(pCap->hContainer);
if(pOne)
{
if(pOne->ItemType == pCapData->Header.ItemType)
{
void *pAppValue = malloc(AltTWItemSize(pCapData->Header.ItemType));

if(pAppValue)
{
TW_UINT16 twSize = AltTWItemSize(pCapData->Header.ItemType);
ExtractOneValue(pCap, pAppValue);

if(pOne->ItemType==TWTY_FIX32)
{
//I THINK THIS IS WHERE THE VALUE IS SET
//CAPSLOCK FOR BETTER VISIBILITY
*(float*)pAppValue = FIX32ToFloat(*(pTW_FIX32)pAppValue);
twSize = sizeof(float);
}

if(IsItemInList(&pCapData->DefaultSupported, pAppValue, twSize))
{
memcpy(pCapData->CurrentValue.pItems, pAppValue, twSize);
twRc = TWRC_SUCCESS;
*pStatus = TWCC_SUCCESS;

free(pAppValue);
pAppValue = NULL;

GlobalUnlock(pCap->hContainer);
break;
}
free(pAppValue);
pAppValue = NULL;
}
}
GlobalUnlock(pCap->hContainer);
}
return twRc;
}
VOID ExtractOneValue (pTW_CAPABILITY pData, LPVOID pVoid)
{
pTW_ONEVALUE pOneValue = NULL;

ASSERT(pData);
ASSERT(pVoid);
ASSERT(pData->ConType == TWON_ONEVALUE);

if ((pOneValue = (pTW_ONEVALUE)GlobalLock(pData->hContainer)) != NULL)
{
switch (pOneValue->ItemType)
{

case TWTY_FIX32:
*(float *)pVoid = FIX32ToFloat(*(pTW_FIX32)&pOneValue->Item);
break;

default:
break;
}

GlobalUnlock(pData->hContainer);
}

return;
}
</C>

Big thanks in advance for help

best regards
Mateusz [PEYN] Adamus
Nov 16 '05 #1
5 4284
Hi again

Some additional help. I can put single value into TW_ONEVALUE - just
like it is done in the example from www.codeproject.com.

<C#>
Cap = (short) cap;
ConType = (short) TwOn.One;

Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
Marshal.WriteInt16(pv, 0, (short) typ);
Marshal.WriteInt32(pv, 2, sval);
Twain.GlobalUnlock(Handle);
</C#>

sval is one value (i.e. number)

But how to put there a pointer to a TW_FIX32 structure?

best regards
Mateusz [PEYN] Adamus
Nov 16 '05 #2
Hi Mateusz,

Not 100% sure, but this may be somewhat C# style of doing it.

You did not provide code for FIX32ToFloat or pCap_STATESTORAGEITEM which
might have led to an even simpler solution.

struct TW_ONEVALUE
{
public UInt16 ItemType;
public UInt32 Item;
}

struct TW_CAPABILITY
{
public UInt16 Cap;
public UInt16 ConType;
public TW_ONEVALUE Container;
}

struct TW_FIX32
{
public Int16 Whole;
public UInt16 Frac;
}

UInt16 HandleSetCap(Cap_STATESTORAGEITEM pCapData, TW_CAPABILITY pCap, ref
UInt16 pStatus)
{
int i = 0;
UInt16 twRc = TWRC_FAILURE;
pStatus = TWCC_BADVALUE;

TW_ONEVALUE pOne = pCap.Container;
if(pOne.ItemType == pCapData.Header.ItemType)
{
UInt32 pAppValue;

// ExtractOneValue(pCap, ref pAppValue)
// Not necessary as all it does is ...

pAppValue = pCap.Container.Item;

if(pOne.ItemType == TWTY_FIX32)
{
//not exactly sure what happens here
//Convert the UInt32 to float?
}

if(IsItemInList(pCapData.DefaultSupported, pAppValue))
{
pCapData.CurrentValue.pItems = /*(float)*/pAppValue;
twRc = TWRC_SUCCESS;
pStatus = TWCC_SUCCESS;
}
}

return twRc;
}
void ExtractOneValue(TW_CAPABILITY pData, ref UInt32 pVoid)
{
TW_ONEVALUE pOneValue = pData.Container;

switch(pOneValue.ItemType)
{
case TWTY_FIX32:
pVoid = pOneValue.Item;
break;
default:
break;
}
return;
}

Big thanks in advance for help

best regards
Mateusz [PEYN] Adamus


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
Hi

Thanks for answering my post.
I can get values from the function but I have a problem with sending
proper to one. There is no problem when data which I want to send is a
simple number. But if there is a structure I can't do it :-(

Simple value I send using the NETMaster's way:
<C#>
[StructLayout(LayoutKind.Sequential, Pack=2)]
internal class TwCapability
{ // TW_CAPABILITY
public short Cap;
public short ConType;
public IntPtr Handle;

public TwCapability(TwCap cap, int sval, TwType typ)
{
Cap = (short) cap;
ConType = (short) TwOn.One;

Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
Marshal.WriteInt16(pv, 0, (short) typ);
Marshal.WriteInt32(pv, 2, sval);
Twain.GlobalUnlock(Handle);
}
}
</C#>

But I don't know how to insert TW_FIX32 structure into sval place. I
think there should be pointer inserted but I don't know how :-(

TW_FIX32 structure looks like this:

<C#>
[StructLayout(LayoutKind.Sequential, Pack=2)]
internal class TwFix32
{ // TW_FIX32
public short Whole;
public ushort Frac;

public float ToFloat()
{
return (float) Whole + ( (float)Frac /65536.0f );
}

public void FromFloat(float f)
{
int i = (int)((f * 65536.0) + 0.5);
Whole = (short) (i >> 16);
Frac = (ushort) (i & 0x0000ffffL);
}
}
</C#>

and TW_ONEVALUE struct looks like this:

<C#>
[StructLayout(LayoutKind.Sequential, Pack=2)]
internal class TwOneValue
{
public short ItemType;
public int Item;
}
</C#>

OK. Hope you'll have time for answering my mail.

If you'll have any qestions write.

best regards
Mateusz [PEYN] Adamus

PS: I wrote to your email but it returned my message :-(
Nov 16 '05 #4
Ah, marshalling, that complicates matters and I'm afraid I have little
experience with it, especially when pointers and unknown structure size is
involved.

You may have better luck translating from Java instead of C. If your code
is related to obtaining images using TWAIN, maybe this Java sample can
shed some light.

http://today.java.net/pub/a/today/2004/11/18/twain.html
http://today.java.net/pub/a/today/2005/01/25/twain.html

Sorry about the e-mail, it is not checked and functions merely as a
signature.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5
Morten Wennevik wrote(a):
Ah, marshalling, that complicates matters and I'm afraid I have little
experience with it, especially when pointers and unknown structure size
is involved.

You may have better luck translating from Java instead of C. If your
code is related to obtaining images using TWAIN, maybe this Java sample
can shed some light.

http://today.java.net/pub/a/today/2004/11/18/twain.html
http://today.java.net/pub/a/today/2005/01/25/twain.html

Hi

Thanks for the tutorial. Unfortunatly there isn't much more than in that
article made by NETMaster.

Anyways thanks for trying :-)

Meybe someone else on this group know something about marshaling?

best regards
Mateusz [PEYN] Adamus
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matt | last post by:
Short Description: The Translation Hub is not picking up the TNS names file. I'm using Oracle 9ids. It doesn't work for any users and it has never worked since it's been installed. Long...
40
by: Chiwa | last post by:
Hey, Expression: Math.floor(x * 100) / 100 x= 4.1 gives 4.09, why in gods name? While other values for x don't give a problem. Thx in advance
5
by: Haines Brown | last post by:
I suspect this is a FAQ, but as a guide to my further investigation, I wondered what the options are for providing readers of my web pages with the ability to translate the pages into their own...
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
7
by: Peter Eisentraut | last post by:
Greetings. The official PostgreSQL documentation has been translated to German and has been published as a book by verlag moderne industrie/mitp ("PostgreSQL: Das offizielle Handbuch", ISBN...
4
by: Chris Croughton | last post by:
Does a translation unit have to have at least one externally visible declaration or function definition to be valid? As I read the standard, it doesn't: it must have at least one declaration or...
2
by: James | last post by:
In oracle I can do the following: select CONVERT(COLUMNOFNAMES,'US7ASCII') from mytable -- It will take a name like Albrecht Dürer and change it into Albrecht Durer and is useful in where...
1
by: dilabox | last post by:
Hello, I have overloaded the global new, delete, new and delete operators inside a "static library". The library uses private memory allocation routines that must not be accessible from other...
4
by: Tomás Ó hÉilidhe | last post by:
I have a translation unit which defines a global object: BEGIN "yellow.c" int i = 7; END I want this object to be accessible from other translation units, so I give it external linkage....
0
by: Stef Mientki | last post by:
hello, I've build a translation tool, to translate all strings in a python source file. As a extra gadget I added translation through Babel Fish, using beautifulsoup. Although it works...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.