469,646 Members | 1,672 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

DllImport / unmanaged code

Jim
I am extremely frustrated. I am building c# application
for a call center and am using a third party API to access
some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once
I call the class from inside a c# windows application the
program freezes, crashes etc...

there must be a way to make this work inside a windows app
as it run great as a command line app.

Please any ideas would be welcomed at this trying time in
my life

Jim
Nov 15 '05 #1
15 4158
Are you using win98?

Does it use System.Timers.Timer on Win98? I found that there appears to be
a threadpool problem(my guess here) in win98 with these that causes the
timers (all of them) to freeze. The main ui still responds, but the timer
events stop. I replaced the System.Timers.Timer with
System.Windows.Forms.Timer and the application started working. This was
with .NET 1.1.

-Larry

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:07****************************@phx.gbl...
I am extremely frustrated. I am building c# application
for a call center and am using a third party API to access
some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once
I call the class from inside a c# windows application the
program freezes, crashes etc...

there must be a way to make this work inside a windows app
as it run great as a command line app.

Please any ideas would be welcomed at this trying time in
my life

Jim

Nov 15 '05 #2
Jim
I am using XP Pro on all machines, client and development.

I am using a Timer, and I will test this with the timers
off and post the findings. The timer I am using is the
System.Windows.Forms.Timer.

-----Original Message-----
Are you using win98?

Does it use System.Timers.Timer on Win98? I found that there appears to bea threadpool problem(my guess here) in win98 with these that causes thetimers (all of them) to freeze. The main ui still responds, but the timerevents stop. I replaced the System.Timers.Timer with
System.Windows.Forms.Timer and the application started working. This waswith .NET 1.1.

-Larry

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:07****************************@phx.gbl...
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once I call the class from inside a c# windows application the program freezes, crashes etc...

there must be a way to make this work inside a windows app as it run great as a command line app.

Please any ideas would be welcomed at this trying time in my life

Jim

.

Nov 15 '05 #3
Jim
removed all timers and still crashing.

extremely frustrating as this is the same class I use as a
command line appliation.

-----Original Message-----
Are you using win98?

Does it use System.Timers.Timer on Win98? I found that there appears to bea threadpool problem(my guess here) in win98 with these that causes thetimers (all of them) to freeze. The main ui still responds, but the timerevents stop. I replaced the System.Timers.Timer with
System.Windows.Forms.Timer and the application started working. This waswith .NET 1.1.

-Larry

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:07****************************@phx.gbl...
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once I call the class from inside a c# windows application the program freezes, crashes etc...

there must be a way to make this work inside a windows app as it run great as a command line app.

Please any ideas would be welcomed at this trying time in my life

Jim

.

Nov 15 '05 #4
Jim
Ok, I have been researching and I just found an
interesting statement. PINVOKE is designed for WIN32 API
calls only. Is this true? I have trying to use PInvoke
to call a third party API Inside the WIN32 System
directory. Could this be causing my error?

Jim
-----Original Message-----
I am extremely frustrated. I am building c# application
for a call center and am using a third party API to accesssome hardware. When develop and test my class using the
windows console the application runs flawlessly, but once
I call the class from inside a c# windows application the
program freezes, crashes etc...

there must be a way to make this work inside a windows appas it run great as a command line app.

Please any ideas would be welcomed at this trying time in
my life

Jim
.

Nov 15 '05 #5
Some possibilities come to mind:
1. does your code call any controls on the WinForms portion from another
thread?
2. I recently had a problem where the unmanaged code was stepping on memory.
It gave results similar to what you are seeing. It ran fine for a long time,
but when I introduced some new forms (and another thread) the error began
causing problems. It only showed up when a form was loaded. But the error
had nothing to do with the form. It was related to the unmanaged code
stepping on memory (an array issue).

In general, calling unmanaged code from a WinForms app works fine. So you
must have a bug somewhere.

I hope those ideas help.

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:07****************************@phx.gbl...
I am extremely frustrated. I am building c# application
for a call center and am using a third party API to access
some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once
I call the class from inside a c# windows application the
program freezes, crashes etc...

there must be a way to make this work inside a windows app
as it run great as a command line app.

Please any ideas would be welcomed at this trying time in
my life

Jim

Nov 15 '05 #6
No. PInvoke can call (virtually) any DLL. I've called C and C++ DLLs of many
different types, some not even written specifically for the latest/newest
versions of Windows OS's.

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:02****************************@phx.gbl...
Ok, I have been researching and I just found an
interesting statement. PINVOKE is designed for WIN32 API
calls only. Is this true? I have trying to use PInvoke
to call a third party API Inside the WIN32 System
directory. Could this be causing my error?

Nov 15 '05 #7
Jim,

It's possible with P/Invoke to define things so that that they work
sometimes, but stop working if the environment changes a little. Can you
post the P/Invoke code you've written (both the functions and the calls),
along with the headers for the functions you're calling?

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Jim" <je******@nationsfirstcorp.com> wrote in message
news:02****************************@phx.gbl...
Ok, I have been researching and I just found an
interesting statement. PINVOKE is designed for WIN32 API
calls only. Is this true? I have trying to use PInvoke
to call a third party API Inside the WIN32 System
directory. Could this be causing my error?

Jim
-----Original Message-----
I am extremely frustrated. I am building c# application
for a call center and am using a third party API to

access
some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once
I call the class from inside a c# windows application the
program freezes, crashes etc...

there must be a way to make this work inside a windows

app
as it run great as a command line app.

Please any ideas would be welcomed at this trying time in
my life

Jim
.

Nov 15 '05 #8
Jim
Here is the code

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;

namespace MarketingRep
{
/// <summary>
/// Summary description for Pika.
/// </summary>
public class Pika
{

/**************************************************
*************
* Create a delegate for our callback
function.
*
************************************************** *********
*/
unsafe public delegate void EventHandlerCB
(void* vpUserData, uint uiEvent, uint uiParam0, uint
uiParam1);
/**************************************************
************
* enumeration for networkinterface types
for pika voice card
* we are using a headset, value 5
*
************************************************** *********
*/
public enum TNetworkInterfaceType
{
PK_CONFIG_NI_NONE
= 0,
PK_CONFIG_NI_LS_TRUNK
= 1,
PK_CONFIG_NI_PHONE
= 2,
PK_CONFIG_NI_DID_TRUNK
= 3,
PK_CONFIG_NI_EM_TRUNK
= 4,
PK_CONFIG_NI_HEADSET
= 5,
PK_CONFIG_NI_DIGITAL
= 6
}

/*
* Trunk device states
*/
public enum TTrunkHookState
{
PK_TRUNK_INVALID,
PK_TRUNK_OFFHOOKING,
PK_TRUNK_OFFHOOK,
PK_TRUNK_ONHOOKING,
PK_TRUNK_ONHOOK,
PK_TRUNK_HOOKFLASHING,
PK_TRUNK_DIALING,
PK_TRUNK_WINKING,
PK_TRUNK_GROUND_BUTTON
}
/**************************************************
************
* GROUP based Voice Conference Definitions
*
************************************************** *********
*/

public enum TGroupMode
{
PK_VC_GroupMode_Sampling
= 0,
PK_VC_GroupMode_Summation
= 1
}

public struct TGroupParameters
{
public TGroupMode groupMode;
}

/**************************************************
****************
* Event struct for callback funtion

************************************************** *********
******/
public struct SEventQ
{
public uint uiUserData;
public uint uiEvent;
public uint uiParam0;
public uint uiParam1;
}
SEventQ[] EvQ = new SEventQ[200];
// int iQOut = 0;
int iQIn = 0;

[DllImport("PikaAPI.dll")]
public static extern uint
PK_CTI_NumberOfBoards();

[DllImport("PikaAPI.dll")]
public static extern uint PK_CTI_Initialize
(uint TBoardIndex);

[DllImport("PikaAPI.dll")]
public static extern uint PK_CTI_Start
(uint TBoardIndex);

[DllImport("PikaAPI.dll")]
public static extern uint PK_CTI_StopDriver
(uint TBoardIndex);

[DllImport("PikaAPI.dll")]
unsafe public static extern int
PK_BOARD_SetEventHandler(uint TResourceHandle, void*
PK_VOID, EventHandlerCB EventHandler);

[DllImport("PikaAPI.dll")]
unsafe public static extern int
PK_DSP_DEVICE_SetEventHandler(uint TResourceHandle, void*
PK_VOID, EventHandlerCB EventHandler);

[DllImport("PikaAPI.dll")]
public static extern uint
PK_DSP_GetDeviceHandle(uint TBoardIndex, uint PK_Word);

[DllImport("PikaAPI.dll")]
public static extern uint
PK_DSP_DEVICE_GetPortHandle(uint TResourceHandle, uint
PK_WORD);

[DllImport("PikaAPI.dll")]
unsafe public static extern uint
PK_DSP_PORT_SetEventHandler(uint TResourceHandle, void*
PK_VOID, EventHandlerCB EventHandler);

[DllImport("PikaAPI.dll")]
public static extern uint
PK_AUDIO_SetFormat(uint TResourceHandle, uint
TAudioFormat);

[DllImport("PikaAPI.dll")]
public static extern uint PK_VC_AddMember
(uint TGroupHandle, uint TResourceHandle, int TGainLevel,
int TGainLevel1, uint PK_BOOL);

[DllImport("PikaAPI.dll")]
unsafe public static extern uint
PK_VC_SetGroupParameters(uint TGroupHandle, void*
TGroupParameters);

[DllImport("PikaAPI.dll")]
public static extern uint PK_VC_CreateGroup
(uint TDeviceHandle);

[DllImport("PikaAPI.dll")]
public static extern uint
PK_PHONE_SeizeResource(uint TBoardIndex,
TNetworkInterfaceType interfaceEnum);

[DllImport("PikaAPI.dll")]
unsafe public static extern uint
PK_PHONE_SetEventHandler(uint TResourceHandle, void*
PK_VOID, EventHandlerCB EventHandler);

[DllImport("PikaAPI.dll")]
public static extern int PK_TRUNK_OffHook
(uint TResourceHandle);

[DllImport("PikaAPI.dll")]
public static extern int PK_TRUNK_OnHook
(uint TResourceHandle);

[DllImport("PikaAPI.dll")]
public static extern uint
PK_TRUNK_CreateResource(uint TBoardIndex, uint PK_WORD);

[DllImport("PikaAPI.dll")]
unsafe public static extern uint
PK_TRUNK_SetEventHandler(uint TResourceHandle, void*
PK_VOID, EventHandlerCB EventHandler);

[DllImport("PikaAPI.dll")]
public static extern uint
PK_TRUNK_CallOfferDetection(uint TResourceHandle, uint
PK_BOOL);

[DllImport("PikaAPI.dll")]
public static extern uint
PK_TRUNK_DisconnectDetection(uint TResourceHandle, uint
PK_BOOL);

[DllImport("PikaAPI.dll")]
public static extern TTrunkHookState
PK_TRUNK_GetHookState(uint TResourceHandle);

/**************************************************
********
* Global Variables
*
************************************************** *****/
uint hDSP = 0;
uint hGroup = 0;
uint hTrunk = 0;
uint uiBoardIndex = 0;
uint hMemberTrunk = 0;
uint hMemberHeadset = 0;
uint hHeadset = 0;
// uint hMemberDialInput = 0;
// uint hMemberDialOutput = 0;
// uint hMemberToneDetectOutput = 0;
// uint hMemberToneDetectInput = 0;
uint uiNumberOfBoards = 0;

const int PK_AUDIO_FORMAT_MU_LAW
= 0x00000020; /* Default */
const int PK_AUDIO_SAMPLING_RATE_8_KHz =
0x00000000; /* Default */

const uint PK_SUCCESS = 0;
const uint PK_TRUE = 1;
const uint PK_FALSE = 0;

//EVENT CODES
const uint PK_EVENT_TRUNK_OFFHOOK = 512;
const uint PK_EVENT_TRUNK_ONHOOK =
0x00000201;
const uint PK_EVENT_TRUNK_BUSY =522;

//RETURN CODES
const int PK_DRV_E_INVALID_RESOURCE = -102;
const int PK_TRUNK_E_NOT_OFFHOOK = -350;
const int PK_TRUNK_E_NOT_ONHOOK = -351;

public Pika()
{
if(InitializeBoard())
{
//Console.Write("Board
Initialized");
}
else
{
//Console.Write("No Board
Found");
}

if(GetResources())
{
//Console.Write("Finished
getting Resource Handles\r\n\r\n");
}
else
{
//Console.Write("Error
getting Resource Handles \r\n");
}
/**************************************************
*****************
* start the process
*
************************************************** *********
****/

if(PK_VC_AddMember(hGroup,
hMemberTrunk, 240, 0, PK_FALSE) == PK_SUCCESS)
{
if(PK_VC_AddMember(hGroup,
hMemberHeadset, 0,0, PK_FALSE) == PK_SUCCESS)
{
//if
(PK_VC_AddMember(hGroup, hMemberDialInput, 0,0, PK_FALSE)
== PK_SUCCESS)
//{
// if
(PK_VC_AddMember(hGroup, hMemberToneDetectOutput, 0,0,
PK_FALSE) == PK_SUCCESS)
// {

TGroupParameters GroupParameters = new
TGroupParameters();

GroupParameters.groupMode = new TGroupMode();

unsafe
{

if(PK_VC_SetGroupParameters(hGroup,
&GroupParameters) == PK_SUCCESS)

{

//Console.Write("Group parameters set");

}

else

{

//Console.Write("Error setting group
Parameters");

}
}
// }
//}
}
else
{
//Console.Write
("Add Member headset error");
}
}
else
{
//Console.Write("Add
member Group error");
}
}
/**************************************************
*******************
*
* InitializeBoard
*
*
************************************************** *********
*******/
private bool InitializeBoard()
{
uiNumberOfBoards =
PK_CTI_NumberOfBoards();

// Check for found board if none
return false;
if(uiNumberOfBoards > 0)
{
// Initialize the board

if(PK_CTI_Initialize
(uiBoardIndex) == 0)
{
//Console.Write
("Board Initialized\r\n");
}
else
{
return false;
}

if(PK_CTI_Start
(uiBoardIndex) == 0)
{
Console.Write("Board
Started\r\n");
}
else
{
return false;
}
return true;
}
else
{
//Console.Write("No Boards
Found\r\n");
return false;
}

}
/**************************************************
****************
* GetResources
* gets the DSP information and sets all
ports for usage
*
************************************************** *********
****/
private bool GetResources()
{ //Set Board Event Handler
unsafe
{
EventHandlerCB EB = new
EventHandlerCB(EventHandler);
if
((PK_BOARD_SetEventHandler(0, (void*)5000, EB)) !=
PK_SUCCESS)
{
//Console.Write
("Could Not set Trunk Event Handler");
return false;
}
else
{
//Console.Write
("DSP Trunk Event Handler set \r\n");
}
}
//Get the handle of DSP 0 on Board
0
if((hDSP = PK_DSP_GetDeviceHandle
(0, uiBoardIndex)) == Convert.ToUInt32(null))
{
return false;
}
else
{
//Console.Write
("\r\nDevice Handle: " + hDSP + "\r\n");
}

//Set Board Event Handler
unsafe
{
EventHandlerCB E = new
EventHandlerCB(EventHandler);
if
((PK_DSP_DEVICE_SetEventHandler(hDSP, (void*)uiBoardIndex,
E)) != PK_SUCCESS)
{
//Console.Write
("Could Not set Trunk Event Handler");
return false;
}
else
{
//Console.Write
("DSP Trunk Event Handler set \r\n");
}
}

/**************************************************
*************
* use this section to get all port
information
* needed for Inline MM to be abel
to assign your input
* and output ports.
*
// Get number of ports
with enabled applications
uint NumPorts =
PK_DSP_DEVICE_NumberOfPorts(hDSP);

//get all port handles
uint[] handleArray = new
uint[NumPorts];
for(uint i = 0; i <
NumPorts; i++)
{
handleArray[i] =
PK_DSP_DEVICE_GetPortHandle(hDSP, i);
}

for(int z=0; z < NumPorts;
z++)
{
//Console.Write
("\r\n" + handleArray[z].ToString());
}
//Console.Write
("\r\n\r\n");
TResourceDesc trd = new
TResourceDesc();
for(uint d = 0; d<
NumPorts; d++)
{
uint y =
handleArray[d];

PK_DSP_PORT_GetResourceDesc(handleArray[d], ref
trd);

//Console.Write(handleArray[d].ToString());

//Console.Write("\r\nSwitch Index: " +
trd.hSwDev.ToString());

//Console.Write("\r\nDevice Handle: " +
trd.hDev.ToString());

//Console.Write("\r\nSwitch Input Port: " +
trd.inPort.ToString());

//Console.Write("\r\nSwitch Output Port: " +
trd.outPort.ToString());

//Console.Write("\r\nResource Mask: " +
trd.resMask.ToString());

//Console.Write("\r\n\r\n");
}

*/

// Get Handle fo Trunk Conference
Port
if((hMemberTrunk =
PK_DSP_DEVICE_GetPortHandle(hDSP, 0)) == Convert.ToUInt32
(null))
{
//Console.Write("Could Not
Seize Trunk Port");
return false;
}
else
{
//Console.Write("Trunk
Port Seied\r\n");
}

//Set trunk event handler
unsafe
{
EventHandlerCB EH = new
EventHandlerCB(EventHandler);
if
((PK_DSP_PORT_SetEventHandler(hMemberTrunk, (void*)
hMemberTrunk, EH)) != PK_SUCCESS)
{
//Console.Write
("Could Not set Trunk Event Handler");
return false;
}
else
{
//Console.Write
("DSP Trunk Event Handler set \r\n");
}
}

//Get handle of headset conference
port
if((hMemberHeadset =
PK_DSP_DEVICE_GetPortHandle(hDSP, 1)) == Convert.ToUInt32
(null))
{
//Console.Write("Could Not
Seize Headset Port");
return false;
}
else
{
//Console.Write("Headset
Port Seized\r\n");
}
//get Output port for DTMF dialing
//
/* if((hMemberDialOutput =
PK_DSP_DEVICE_GetPortHandle(hDSP, 4)) == Convert.ToUInt32
(null))
{
//Console.Write("Could Not
Seize dmtf Port");
return false;
}
else
{
//Console.Write("dmtf Port
Seized\r\n");
}

//Set trunk event handler
unsafe
{
EventHandlerCB EvetHandler
= new EventHandlerCB(EventHandler);
if
((PK_DSP_PORT_SetEventHandler(hMemberDialOutput, (void*)
hMemberDialOutput, EvetHandler)) != PK_SUCCESS)
{
//Console.Write
("Could Not set dmtf Event Handler");
return false;
}
else
{
//Console.Write
("DSP dtmf Event Handler set \r\n");
}
}

//get Input port for DTMF dialing
//
if((hMemberDialInput =
PK_DSP_DEVICE_GetPortHandle(hDSP, 15)) == Convert.ToUInt32
(null))
{
//Console.Write("Could Not
input Seize dmtf Port");
return false;
}
else
{
//Console.Write("dmtf
input Port Seized\r\n");
}

//Set trunk event handler
unsafe
{
EventHandlerCB EHan = new
EventHandlerCB(EventHandler);
if
((PK_DSP_PORT_SetEventHandler(hMemberDialInput, (void*)
hMemberDialInput, EHan)) != PK_SUCCESS)
{
//Console.Write
("Could Not set dmtf Event Handler");
return false;
}
else
{
//Console.Write
("DSP dtmf Event Handler set \r\n");
}
}
*/
// SEt Trunk Audio
if((PK_AUDIO_SetFormat
(hMemberTrunk, (PK_AUDIO_FORMAT_MU_LAW |
PK_AUDIO_SAMPLING_RATE_8_KHz))) != PK_SUCCESS)
{
//Console.Write("Could Not
Set Trunk Audio");
return false;
}
else
{
//Console.Write("Trunk
Audio Set\r\n");
}

// Set Headset Audio
if((PK_AUDIO_SetFormat
(hMemberHeadset, (PK_AUDIO_FORMAT_MU_LAW |
PK_AUDIO_SAMPLING_RATE_8_KHz))) != PK_SUCCESS)
{
//Console.Write("Could Not
Set Headset Audio");
return false;
}
else
{
//Console.Write("Headset
Audio Set\r\n");
}
//Set trunk event handler
unsafe
{
EventHandlerCB EHa = new
EventHandlerCB(EventHandler);
if
((PK_DSP_PORT_SetEventHandler(hMemberHeadset, (void*)
hMemberHeadset, EHa)) != PK_SUCCESS)
{
//Console.Write
("Could Not set Trunk Event Handler");
return false;
}
else
{
//Console.Write
("DSP Trunk Event Handler set \r\n");
}
}

// Get Handle for conference group
if((hGroup = PK_VC_CreateGroup
(hDSP)) == Convert.ToUInt32(null))
{
//Console.Write("Could set
Conference group");
return false;
}
else
{
//Console.Write
("Conference group set");
}

//get headset handle of trunk 0 on
board 0
// seize headset handle get trunk
1 board 0
TNetworkInterfaceType IType = new
TNetworkInterfaceType();
IType = (TNetworkInterfaceType)5;
if((hHeadset =
PK_PHONE_SeizeResource(uiBoardIndex, IType)) ==
Convert.ToUInt32(null))
{
//Console.Write("ERROR
Retrieving Headset Handle");
return false;
}
else
{
//Console.Write("Headset
Handle: " + hHeadsetHandle + "\r\n");
}

// Get second port for tone
Detection.
/* //Tone Detection input
if((hMemberToneDetectInput =
PK_DSP_DEVICE_GetPortHandle(hDSP, 16)) == Convert.ToUInt32
(null))
{
//Console.Write("Could Not
input Seize Tone Detect Port");
return false;
}
else
{
//Console.Write("tone
detection input Port Seized\r\n");
}

//Set trunk event handler
unsafe
{
EventHandlerCB EvetHandle
= new EventHandlerCB(EventHandler);
if
((PK_DSP_PORT_SetEventHandler(hMemberToneDetectInp ut,
(void*)hMemberToneDetectInput, EvetHandle)) != PK_SUCCESS)
{
//Console.Write
("Could Not set tone dectetion input Event Handler");
return false;
}
else
{
//Console.Write
("DSP tone detect input Event Handler set \r\n");
}
}

// Tone detection output.
//
if((hMemberToneDetectOutput =
PK_DSP_DEVICE_GetPortHandle(hDSP, 5)) == Convert.ToUInt32
(null))
{
//Console.Write("Could Not
iOutput Seize Tone Detect Port");
return false;
}
else
{
//Console.Write("tone
detection Output Port Seized\r\n");
}

//Set trunk event handler
unsafe
{
EventHandlerCB EHand = new
EventHandlerCB(EventHandler);
if
((PK_DSP_PORT_SetEventHandler(hMemberToneDetectOut put,
(void*)hMemberToneDetectOutput, EHand)) != PK_SUCCESS)
{
//Console.Write
("Could Not set tone dectetion Output Event Handler");
return false;
}
else
{
//Console.Write
("DSP tone detect Output Event Handler set \r\n");
}
}
//Load the tone file onto the DSP
int iErrorCode = 0;
if((iErrorCode =
PK_VP_SetToneAllGroupsDescFromFile(hDSP, "C:\\Documents
and Settings\\Jim English\\My Documents\\Visual Studio
Projects\\Pika_Test\\Tone_file.tb")) != PK_SUCCESS)
{
//Console.Write("Could Not
UPLOAD TONE FILE " + iErrorCode + "\r\n");
return false;
}
else
{
//Console.Write("TONE FILE
UPLOADED \r\n");
}

//Set the tones ont he ports
if(PK_VP_IncludeToneEx
(hMemberToneDetectInput, TONE_DIALTONE) != PK_SUCCESS)
{
//Console.Write("Dial Tone
not loaded \r\n");
return false;
}
else
{
//Console.Write("DIal tone
loaded \r\n");
}

if(PK_VP_IncludeToneEx
(hMemberToneDetectInput, TONE_BUSY) != PK_SUCCESS)
{
////Console.Write("BUSY
Tone not loaded \r\n");
return false;
}
else
{
//Console.Write("BUSY tone
loaded \r\n");
}

if(PK_VP_IncludeToneEx
(hMemberToneDetectInput, TONE_RINGBACK) != PK_SUCCESS)
{
//Console.Write("RINGBACK
Tone not loaded \r\n");
return false;
}
else
{
//Console.Write("RINGBACK
tone loaded \r\n");
}
/**************************************************
******************
* enable tone
dection
*/ // * ****/
// PK_VP_EnableToneDetection
(hMemberToneDetectInput);

// get handle of trunk 0 on board 0
if((hTrunk =
PK_TRUNK_CreateResource(uiBoardIndex, 0)) ==
Convert.ToUInt32(null))
{
//Console.Write("Error
getting trunk handle");
return false;
}
else
{
//Console.Write("Trunk
handle retrieved\r\n");
}
// SetTrunk event handler
unsafe
{
EventHandlerCB EvtHandl =
new EventHandlerCB(EventHandler);
if
((PK_TRUNK_SetEventHandler(hTrunk, (void*)hTrunk,
EvtHandl)) != PK_SUCCESS)
{
//Console.Write
("Could Not Trunk Event Handler");
return false;
}
else
{
//Console.Write
("DSP Trunk Event Handler set \r\n");
}
}

if((PK_TRUNK_DisconnectDetection
(hTrunk, PK_TRUE)) != PK_SUCCESS)
{
return false;
}
else
{
//Console.Write("Trunk
Disconnection Enabled \r\n");
}

if((PK_TRUNK_CallOfferDetection
(hTrunk, PK_TRUE)) != PK_SUCCESS)
{
return false;
}
else
{
//Console.Write("Trunk
Call Offer Detection Enabled \r\n");

}
return true;

}
/**************************************************
*********************
* EventHandler
* Callback function for Pika API
*
************************************************** *********
*********/
unsafe public void EventHandler (void*
vpUserData, uint uiEvent, uint uiParam0, uint uiParam1)
{
lock(EvQ)
{
EvQ[iQIn].uiUserData =
(uint)vpUserData;
EvQ[iQIn].uiEvent =
uiEvent;
EvQ[iQIn].uiParam0 =
uiParam0;
EvQ[iQIn].uiParam1 =
uiParam1;

//Console.Write("\r\nEvent
Handler " + iQIn.ToString());
//Console.Write
("\r\nEvent : " + uiEvent.ToString() + "\r\n");

iQIn ++;

if(iQIn == 200) //Queue at
max compacity
{
iQIn = 0; //Reset
to 0
}
}
}
public string getHookState()
{
TTrunkHookState th = new
TTrunkHookState();
th = PK_TRUNK_GetHookState(hTrunk);
return th.ToString();
}

public string offHook()
{
int iError = 0;

TTrunkHookState th = new
TTrunkHookState();
th = PK_TRUNK_GetHookState(hTrunk);
if((iError = PK_TRUNK_OffHook
(hTrunk)) != PK_SUCCESS)
{
if(iError ==
PK_DRV_E_INVALID_RESOURCE)

return "PK_DRV_E_INVALID_RESOURCE";

else if(iError ==
PK_TRUNK_E_NOT_ONHOOK)

return "PK_TRUNK_E_NOT_ONHOOK";

else
return "ERROR -
NOT DEFINED";
}
else
return "Off Hook!";
}
public string onHook()
{
int iE = 0;

if((iE = PK_TRUNK_OnHook(hTrunk)) !
= PK_SUCCESS)
{
if(iE ==
PK_DRV_E_INVALID_RESOURCE)

return "PK_DRV_E_INVALID_RESOURCE";

else if(iE ==
PK_TRUNK_E_NOT_ONHOOK)

return "PK_TRUNK_E_NOT_ONHOOK";

else
return "ERROR -
NOT DEFINED";
}
else
return "On Hook!";
}
}
}
-----Original Message-----
Jim,

It's possible with P/Invoke to define things so that that they worksometimes, but stop working if the environment changes a little. Can youpost the P/Invoke code you've written (both the functions and the calls),along with the headers for the functions you're calling?

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights."Jim" <je******@nationsfirstcorp.com> wrote in message
news:02****************************@phx.gbl...
Ok, I have been researching and I just found an
interesting statement. PINVOKE is designed for WIN32 API calls only. Is this true? I have trying to use PInvoke
to call a third party API Inside the WIN32 System
directory. Could this be causing my error?

Jim
>-----Original Message-----
>I am extremely frustrated. I am building c# application >for a call center and am using a third party API to

access
>some hardware. When develop and test my class using the >windows console the application runs flawlessly, but once >I call the class from inside a c# windows application the >program freezes, crashes etc...
>
>there must be a way to make this work inside a windows

app
>as it run great as a command line app.
>
>Please any ideas would be welcomed at this trying time in >my life
>
>Jim
>.
>

.

Nov 15 '05 #9
Thanks, I am getting the same results, a Null Reference
Exception or the window disappears and the program is
still running.

I have striped down the windows form to two buttons and a
textbox. and post events to the textbox and one button
takes the phone off hook, the other puts the phone back on
hook.

Thaks for the reply....

jIm
-----Original Message-----
Some possibilities come to mind:
1. does your code call any controls on the WinForms portion from anotherthread?
2. I recently had a problem where the unmanaged code was stepping on memory.It gave results similar to what you are seeing. It ran fine for a long time,but when I introduced some new forms (and another thread) the error begancausing problems. It only showed up when a form was loaded. But the errorhad nothing to do with the form. It was related to the unmanaged codestepping on memory (an array issue).

In general, calling unmanaged code from a WinForms app works fine. So youmust have a bug somewhere.

I hope those ideas help.

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:07****************************@phx.gbl...
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once I call the class from inside a c# windows application the program freezes, crashes etc...

there must be a way to make this work inside a windows app as it run great as a command line app.

Please any ideas would be welcomed at this trying time in my life

Jim

.

Nov 15 '05 #10
I see the following using statement in the code you posted for Eric:
using System.Threading;

So I assume you have more than one thread. I also assume you know the rule
that any Windows control must only be updated from the main thread. If you
need more detail, let me know.

Regards,
Mountain

<an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
Thanks, I am getting the same results, a Null Reference
Exception or the window disappears and the program is
still running.

I have striped down the windows form to two buttons and a
textbox. and post events to the textbox and one button
takes the phone off hook, the other puts the phone back on
hook.

Thaks for the reply....

jIm
-----Original Message-----
Some possibilities come to mind:
1. does your code call any controls on the WinForms

portion from another
thread?
2. I recently had a problem where the unmanaged code was

stepping on memory.
It gave results similar to what you are seeing. It ran

fine for a long time,
but when I introduced some new forms (and another thread)

the error began
causing problems. It only showed up when a form was

loaded. But the error
had nothing to do with the form. It was related to the

unmanaged code
stepping on memory (an array issue).

In general, calling unmanaged code from a WinForms app

works fine. So you
must have a bug somewhere.

I hope those ideas help.

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:07****************************@phx.gbl...
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the
windows console the application runs flawlessly, but once I call the class from inside a c# windows application the program freezes, crashes etc...

there must be a way to make this work inside a windows app as it run great as a command line app.

Please any ideas would be welcomed at this trying time in my life

Jim

.

Nov 15 '05 #11
Jim
Please I need more detail.

I am calling a class in the mainform and using the buttons
on the main form also. But I am kinda new to windows,
mostly been doing ASP.NET stuff for 1.5 years.

I even got a System.Runtime.InteropServices.SEHException'
error. Search internet but couldn't find any good
information.

Thanks,
Jim

-----Original Message-----
I see the following using statement in the code you posted for Eric:using System.Threading;

So I assume you have more than one thread. I also assume you know the rulethat any Windows control must only be updated from the main thread. If youneed more detail, let me know.

Regards,
Mountain

<an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
Thanks, I am getting the same results, a Null Reference
Exception or the window disappears and the program is
still running.

I have striped down the windows form to two buttons and a textbox. and post events to the textbox and one button
takes the phone off hook, the other puts the phone back on hook.

Thaks for the reply....

jIm
>-----Original Message-----
>Some possibilities come to mind:
>1. does your code call any controls on the WinForms

portion from another
>thread?
>2. I recently had a problem where the unmanaged code was
stepping on memory.
>It gave results similar to what you are seeing. It ran

fine for a long time,
>but when I introduced some new forms (and another
thread) the error began
>causing problems. It only showed up when a form was

loaded. But the error
>had nothing to do with the form. It was related to the

unmanaged code
>stepping on memory (an array issue).
>
>In general, calling unmanaged code from a WinForms app

works fine. So you
>must have a bug somewhere.
>
>I hope those ideas help.
>
>"Jim" <je******@nationsfirstcorp.com> wrote in message
>news:07****************************@phx.gbl...
>> I am extremely frustrated. I am building c#

application
>> for a call center and am using a third party API to

access
>> some hardware. When develop and test my class using
the >> windows console the application runs flawlessly, but

once
>> I call the class from inside a c# windows application

the
>> program freezes, crashes etc...
>>
>> there must be a way to make this work inside a

windows app
>> as it run great as a command line app.
>>
>> Please any ideas would be welcomed at this trying
time in
>> my life
>>
>> Jim
>
>
>.
>

.

Nov 15 '05 #12
Jim
Mountain,

forgot to mention the Systems.Threading was there for the
lock() method. I needed a critical section for my event
queue and couldn't find anything else to make it happen
quikly in c#.

so I took it out for debuggin purposes after you reply and
still got the error. I think th eproblem may be in my
Callback Function.

JIm
-----Original Message-----
I see the following using statement in the code you posted for Eric:using System.Threading;

So I assume you have more than one thread. I also assume you know the rulethat any Windows control must only be updated from the main thread. If youneed more detail, let me know.

Regards,
Mountain

<an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
Thanks, I am getting the same results, a Null Reference
Exception or the window disappears and the program is
still running.

I have striped down the windows form to two buttons and a textbox. and post events to the textbox and one button
takes the phone off hook, the other puts the phone back on hook.

Thaks for the reply....

jIm
>-----Original Message-----
>Some possibilities come to mind:
>1. does your code call any controls on the WinForms

portion from another
>thread?
>2. I recently had a problem where the unmanaged code was
stepping on memory.
>It gave results similar to what you are seeing. It ran

fine for a long time,
>but when I introduced some new forms (and another
thread) the error began
>causing problems. It only showed up when a form was

loaded. But the error
>had nothing to do with the form. It was related to the

unmanaged code
>stepping on memory (an array issue).
>
>In general, calling unmanaged code from a WinForms app

works fine. So you
>must have a bug somewhere.
>
>I hope those ideas help.
>
>"Jim" <je******@nationsfirstcorp.com> wrote in message
>news:07****************************@phx.gbl...
>> I am extremely frustrated. I am building c#

application
>> for a call center and am using a third party API to

access
>> some hardware. When develop and test my class using
the >> windows console the application runs flawlessly, but

once
>> I call the class from inside a c# windows application

the
>> program freezes, crashes etc...
>>
>> there must be a way to make this work inside a

windows app
>> as it run great as a command line app.
>>
>> Please any ideas would be welcomed at this trying
time in
>> my life
>>
>> Jim
>
>
>.
>

.

Nov 15 '05 #13

"Jim" <je******@nationsfirstcorp.com> wrote in message
news:0c****************************@phx.gbl...
Please I need more detail.


There are four methods on a control that are safe to call from any thread:
Invoke, BeginInvoke, EndInvoke, and CreateGraphics. For all other method
calls, you should use one of the invoke methods to marshal the call to the
control's thread.

You should also read this if you haven't already:
http://msdn.microsoft.com/library/de...ms06112002.asp

And Chris Sells's book, Windows Forms Programming in C# is great for
understanding this stuff.

Have you seen:
http://msdn.microsoft.com/library/de...ithvisualc.asp

and
http://msdn.microsoft.com/library/de...ml/vbasync.asp

Hope that helps.
Nov 15 '05 #14
there is a part 2 to Chris Sells's article:
http://msdn.microsoft.com/library/de...ms08162002.asp


You should also read this if you haven't already:
http://msdn.microsoft.com/library/de...ms06112002.asp


Nov 15 '05 #15
Jim
Thanks for the information. I am reading the articles now.

Jim
-----Original Message-----
there is a part 2 to Chris Sells's article:
http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dnforms/html/winforms08162002.asp

You should also read this if you haven't already:

http://msdn.microsoft.com/library/default.asp?

url=/library/en-us/dnforms/html/winforms06112002.asp


.

Nov 15 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Fabien Penso | last post: by
3 posts views Thread by Mark Jerde | last post: by
2 posts views Thread by Brian Anderson | last post: by
6 posts views Thread by Steve | last post: by
9 posts views Thread by =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post: by
3 posts views Thread by Dan Holmes | last post: by
1 post views Thread by Larry | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.