472,378 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4298
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Fabien Penso | last post by:
Hi. I am trying to make this work but I got a weird behavior. I got a very basic system, I call a unmanaged "dllimported" function and give it a structure of callback functions. Sometimes,...
3
by: Mark Jerde | last post by:
I'm sill learning VS .NET 2003, not an expert yet. I'm calling an unmanaged C++ DLL from C# using . When the whole project is done I will be calling a total of 5 C++ DLLs from C#. All the DLLs...
2
by: Brian Anderson | last post by:
Hello, is it possible to use DllImport to call a DLL in ASP.NET ? Or is it necessarry that my DLL has to be copied into \System32 ? My DLL is a native C++ 7.1 DLL (not managed, no COM, no...
6
by: Steve | last post by:
I'm sure I'm missing something basic. I have an class library that uses an unmanaged dll. Let's say the class library is called "CL1.dll" and is located at: and the unmanaged dll is called...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
9
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a win32dll which is a dll to send SMS. I am using Dllimport to call the methods in the dll. In Winform it works well. In Web form the method doesn't work and returns false. (I am debugging...
3
by: Dan Holmes | last post by:
I have this DllImport attribute on an external C++ dll. What i am noticing that the dll isn't unloaded after the static method is complete. I was thinking that when the method (it is a static...
1
by: Larry | last post by:
I have an asp.net application (C#) that calls a web service that uses several c++ dlls which are loaded with DllImport. I put these dlls in a sub-directory off of the bin directory. For example,...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.