Translation from C to C#

  • Thread starter Mateusz [PEYN] Adamus
  • Start date
M

Mateusz [PEYN] Adamus

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
 
M

Mateusz [PEYN] Adamus

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
 
M

Morten Wennevik

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
 
M

Mateusz [PEYN] Adamus

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 :-(
 
M

Morten Wennevik

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.
 
M

Mateusz [PEYN] Adamus

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
 
Top