|
|
Thanks for the comments. I have tested using a simple Win32 C++ console application to drive our DLL, and it works fine when using the worker thread to invoke COM commands to the third party component. So clearly this is something to do with the .Net application.
Okay, I have solved it.
A COM STA uses Windows messages to serialise access to the COM object which might not be thread safe. So when I created a worker thread, the main thread then hung waiting for the worker thread to finish, but it never did, as the main thread hanging blocked the message pump! Instead of the main thread waiting, I now make it periodically run a message pump to ensure that messages are process. That has solved the problem. Yikes. I hate COM!
Thank you for your helpful post.
Added later: I have also looked into removing the worker thread and marshalling the COM interfaces across threads and this works. It has the advantage that I do not have to mess around with a message pump, which is akin to spinning the tyres.
modified 10-Nov-16 10:36am.
|
|
|
|
|
You could try calling CoInitialize[^] in the worker thread before the call to CoCreateInstance .
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
Thanks for the comment. Yes I call CoInitialise, that is not the issue. I wish it was. 
modified 9-Nov-16 3:51am.
|
|
|
|
|
Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid.
I simply post the necessary codelines in hope that someone can put away the wood infront of my head.
Inside a modal dialogue, these are the calls
for (int y=0;y<5;y++)
{
Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
}
The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:
int CBasis::Basistrefferermittlung()
{
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;
static int Einschlagsnummer;
if (Trefferchance>80)
{
if (Zufallstreffer>95)
{
if (m_Bodyguard>1)
{
Einschlagsnummer=20;
m_Bodyguard=m_Bodyguard-1;
return (Einschlagsnummer);
}
else if (m_Bodyguard<1&&m_Palast>1)
{
Einschlagsnummer=21;
m_Palast=m_Palast-1;
return (Einschlagsnummer);
}
}
else if (Zufallstreffer>90&&Zufallstreffer<95)
{
if (m_Fabrik>1)
{
Einschlagsnummer=16;
m_Fabrik=m_Fabrik-1;
return (Einschlagsnummer);
}
}
else if (Zufallstreffer>85&&Zufallstreffer<90)
{
if (m_Scanner>1)
{
Einschlagsnummer=17;
m_Scanner=m_Scanner-1;
return (Einschlagsnummer);
}
}
else if (Zufallstreffer>80&&Zufallstreffer<85)
{
if (m_Kraftwerk>1)
{
Einschlagsnummer=18;
m_Kraftwerk=m_Kraftwerk-1;
return (Einschlagsnummer);
}
}
else if (Zufallstreffer>75&&Zufallstreffer<80)
{
if (m_Zentrale>1)
{
Einschlagsnummer=19;
m_Zentrale=m_Zentrale-1;
return (Einschlagsnummer);
}
}
}
if (m_Schutzwall>1)
{
Einschlagsnummer=1;
m_Schutzwall=m_Schutzwall-1;
return (Einschlagsnummer);
}
else if (m_Gefechtsstand>1)
{
Einschlagsnummer=2;
m_Gefechtsstand=m_Gefechtsstand-1;
return (Einschlagsnummer);
}
else if (m_Geschuetzturm>1)
{
Einschlagsnummer=3;
m_Geschuetzturm=m_Geschuetzturm-1;
return (Einschlagsnummer);
}
else if (m_Drohnen>1)
{
Einschlagsnummer=4;
m_Drohnen=m_Drohnen-1;
return (Einschlagsnummer);
}
else if (m_Marines>1)
{
Einschlagsnummer=5;
m_Marines=m_Marines-1;
return (Einschlagsnummer);
}
else if (m_Werkspolizei>1)
{
Einschlagsnummer=6;
m_Werkspolizei=m_Werkspolizei-1;
return (Einschlagsnummer);
}
else if (m_Unterkunft>1)
{
Einschlagsnummer=7;
m_Unterkunft=m_Unterkunft-1;
return (Einschlagsnummer);
}
else if (m_ErhZentrum>1)
{
Einschlagsnummer=8;
m_ErhZentrum=m_ErhZentrum-1;
return (Einschlagsnummer);
}
else if (m_EinkZentrum>1)
{
Einschlagsnummer=9;
m_EinkZentrum=m_EinkZentrum-1;
return (Einschlagsnummer);
}
else if (m_Bunker>1)
{
Einschlagsnummer=10;
m_Bunker=m_Bunker-1;
return (Einschlagsnummer);
}
else if (m_Lager>1)
{
Einschlagsnummer=11;
m_Lager=m_Lager-1;
m_Lagerpunkte=m_Lagerpunkte-150;
return (Einschlagsnummer);
}
else if (m_Forschung>1)
{
Einschlagsnummer=12;
m_Forschung=m_Forschung-1;
return (Einschlagsnummer);
}
else if (m_MedZentrum>1)
{
Einschlagsnummer=13;
m_MedZentrum=m_MedZentrum-1;
return (Einschlagsnummer);
}
else if (m_Schild>1)
{
Einschlagsnummer=14;
m_Schild=m_Schild-1;
return (Einschlagsnummer);
}
else if (m_Firewall>1)
{
Einschlagsnummer=15;
m_Firewall=m_Firewall-1;
return (Einschlagsnummer);
}
else if (m_Fabrik>1)
{
Einschlagsnummer=16;
m_Fabrik=m_Fabrik-1;
return (Einschlagsnummer);
}
else if (m_Scanner>1)
{
Einschlagsnummer=17;
m_Scanner=m_Scanner-1;
return (Einschlagsnummer);
}
else if (m_Kraftwerk>1)
{
Einschlagsnummer=18;
m_Kraftwerk=m_Kraftwerk-1;
return (Einschlagsnummer);
}
else if (m_Zentrale>1)
{
Einschlagsnummer=19;
m_Zentrale=m_Zentrale-1;
return (Einschlagsnummer);
}
else if (m_Bodyguard>1)
{
Einschlagsnummer=20;
m_Bodyguard=m_Bodyguard-1;
return (Einschlagsnummer);
}
else if (m_Palast>1)
{
Einschlagsnummer=21;
m_Palast=m_Palast-1;
return (Einschlagsnummer);
};
}
The array of Schadensnummer[5] always return 1 (Einschlagsnummer==1) out of Basistrefferermittlung. But the class KlendautKlendaut_Gegner[0].m_Schutzwall ist declared only 1 at this point and it is reduced in Basistrefferermittlung. So in the second call of this function it must be 0 and so the Schadensnummer[1] has to be 2. But it stays 1 for every instance of Schadensnummer[].
Does someone has a suggestion?
|
|
|
|
|
Use a debugger and step through the function. If a variable's value is not what you are expecting it is a lot easier to find out why by watching when it changes than by peering through the code looking for clues.
|
|
|
|
|
I did that. Debugger s showing the array is filled with "1" in each of the 5 instances.
|
|
|
|
|
You need to delve deeper in the debugger - look at where the array elements are being set to 1, and you should see why (Use F11 to step instead of F10).
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|
|
Hi,
i made a step per step view on the code. It runs through "Schadensnummer[y]=RestonaGuldon_Gegner[0].Basistrefferermittlung();"
and returns 1 after it doesn't enter the line it should
if (m_Schutzwall>1)
{
Einschlagsnummer=1;
m_Schutzwall=m_Schutzwall-1;
return (Einschlagsnummer);
}
it should enter there, because the class array object has .m_Schutzwall>1.
But it neither enters and sets Einschlagsnummer to 1 or subtracts the m_Schutzwall by 1.
There must be something wrong with my declaration of the array of the class "RestonaGuldon_Gegner[0]" oder initialization of the function "Basistrefferermittlung()". But i can't see what it is. I'm blindfolded. I just did everything how i am used to do and it worked fine till now.
I am stunned.
|
|
|
|
|
Nowhere in your code can we see the defintion of "m_Schutzwall" it would be useful
Are you sure you don't have two, m_Schutzwall's it's quite common to that and easy to miss.
If you turn the compiler up to W4 maximum warning it will tell you if m_Schutzwall is hidden by a local copy.
It's dead simple to do something silly with scope like this .. try it
int m_Schutzwall = 1;
int j;
for (j = 1; j < 10; j++){
int m_Schutzwall = j + 2;
}
Do a search for in files for "m_Schutzwall" and look at all the uses.
In vino veritas
|
|
|
|
|
Is it possible that the array int Schadensnummer[] establishs a copy of the member variable of the class RestonaGuldon_Gegner[0]?
The class is global, the variable m_Schutzwall is public.
The class is capsulated in an array.
I dont see a solution so far. it couldnt be that i have to give the array of the class in its own function. i think i forgot something, but i still cant see it.

|
|
|
|
|
addition:
i feel like an idiot. Maybe i am one.
There must be everything allright.
It just misses a ">=" in the function instead of a ">", because the variable is equal 1.
So the entrypoint is never beeing reached. I ll change it and then we ll see.
Thank you so far, i hope i just gave you an evening with something to smile about.
Thanks.
|
|
|
|
|
The title of this thread suggests you are dealing with some function that is returning an array of int s, yet your Basistrefferermittlung() function is simply returning a single int . Is that intentional?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Some issues:
static int Einschlagsnummer;
Is never default initialized.
Some code paths return nothing so whatever is in [e]ax gets returned, which may be 1.
If Trefferchance is <= 80 and m_Schutzwall > 5, it will return 1 for each iteration up to five. However, you stated that m_Schutzwall is 1 which means it's never decremented to zero since the test is for m_Schutzwall > 1, not m_Schutzwall >= 1. This suggests that the return value is likely what happens to be in [e]ax.
now nitpicking:
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;
Why not initialize the variables in the declaration of them?
Why
m_Schutzwall=m_Schutzwall-1; instead of --m_Schutzwall;
|
|
|
|
|
I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:
a b
for input 'a' , 'zz' , output is:
a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz
What is the most efficient way to do it in C?
|
|
|
|
|
There is no efficient way to do it you have to do a brute force roll of each character in each row.
It's a standard odometer setup roll a column, when that is complete move the next column 1.
Essentially it's just two functions roll character, roll next column a character.
The roll next column will on occasion call itself because rolling one column needs to roll the next and next etc (watch an odometer on a car roll)
As this is usually homework for a programming unit I will give you one of the functions, you need to do the other
void RollCharacter (char* MyStr, unsigned short Pos) {
while (MyStr[Pos] <= 'z') {
printf("Permutation string = %s\r\n", MyStr);
MyStr[Pos]++;
}
}
char TestStr[10] = { 0 };
TestStr[0] = 'c';
RollCharacter(TestStr, 0);
The column roll is fractionally harder but not much.
In vino veritas
|
|
|
|
|
I should also say there is a generic form of counting used in encryption that uses sets of characters to numbers.
It isn't fast to code because it requires loops with modulo and divides but it gives you a count in a set of restricted characters or characters in any order.
Consider a set of characters abcABC and that was the digit order like 1,2,3,4,5,6
So counting 0 = a, 1 = b, 2 = c, 3 = A, 4 = B, 5= C, 6 = aa, 7 = ab, 8 = ac, 9 = ba etc
The code to display a count sequence is done like this but I would not call it efficient
static char CodeSet[6] = { 'a', 'b', 'c', 'A', 'B', 'C'};
void CovertCountToCode(char* Buf, unsigned short BufSize, unsigned long Count) {
if ((Buf) && (BufSize)) {
BOOL notComplete = TRUE;
unsigned short column = BufSize - 2;
memset(Buf, 0x20, BufSize);
Buf[BufSize - 1] = '\0';
do {
unsigned short digit = Count % _countof(CodeSet);
Buf[column] = CodeSet[digit];
column--;
if (Count >= _countof(CodeSet)) {
Count /= _countof(CodeSet);
Count--;
} else notComplete = FALSE;
} while ((notComplete) && (column > 0));
}
}
void CountInCode(unsigned short MaxCount) {
char Buf[10];
unsigned short i;
for (i = 0; i < MaxCount; i++) {
CovertCountToCode(Buf, _countof(Buf), i);
printf("Code string = %s\r\n", Buf);
}
}
CountInCode(50);
In vino veritas
|
|
|
|
|
Efficient in terms of speed or lines of code?
If the latter, create a string buffer and then manipulate it until it is greater than the end.
|
|
|
|
|
Hi,
As part of the application development, we need to launch the Printing Preferences window programmatically for the given printer.
We tried using DocumentProperties() API with fmode as DM_IN_PROMPT.
With this we are able to launch the Printing Preferences of V3 Printers.
When we tried for the V4 Printers, we are getting the standard UI provided by the Microsoft, but the custom UI provided by the manufacturer is not displayed.
Is there any way to launch the custom UI of V4 Printers programmatically?
Your valuable answers are highly appreciated.
Best Regards,
PurnaReddy
-- modified 25-Oct-16 0:55am.
|
|
|
|
|
I think you need to talk to the manufacturer, we have no idea what a V4 printer is.
|
|
|
|
|
r[1000];
*r = (int *) malloc ((n+ 1) * sizeof(int));
but if I use int *r then also it doesn't passes properly .Should I use structure?
The problem should take input like
division-3
input of price-1,5,8
and give rod cutting whereas there are three iteration which should be one r=0;
and the maximum price for this division should be 8 but the result comes 1677218
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<limits.h>
int sum=0;
int max(int a, int b)
{
return(a>b)?a:b;
}
int cut_rod_aux(int price[], int n, int r[])
{ int i;
if(r[n]>=0)
{
return r[n];
printf(" %d\n",r[n]);
}
if (n==0)
{
sum=0;
}
else
{
sum =100000;
for( i=1; i<n;i++)
{
sum=max(sum, price[i]+cut_rod_aux(price, n-i,r));
printf("%d \n",sum);
}
}
r[n]=sum;
return sum;
}
int cut_rod(int price[], int n)
{ int i,r[1000];
*r = (int *) malloc ((n+ 1) * sizeof(int));
for( i=0; i<n;i++) {
r[i]=100000;
printf("i=%d\t r=%d \n",i,r[i]);
}
return cut_rod_aux(price,n,r);
}
int main()
{ int arr[100],i, N;
printf("Enter the number of length that will be divided\n");
scanf("%d", &N);
for ( i = 0;i< N; i++) {
printf("Enter price for %d length\n", (i+1));
scanf("%d", &arr[i]);}
printf("output %d \n",cut_rod(arr, N));
return 0;
}
modified 17-Oct-16 3:21am.
|
|
|
|
|
I am assuming your cut and paste failed because as that appears to me that would not compile the r line has no type
Anyhow your malloc is correct the problem is with r
1.) You can't and don't need the array specifier.
2.) You definitely don't dereference the malloc to it ... "*r" means dereference
Anyhow any quick check of creating a dynamic array would give you the correct syntax
int* r;
r = (int *) malloc ((n+ 1) * sizeof(int));
That will allocate n+1 integers holding the allocated array in pointer r.
To access them you then use r[x] where x is the index position in the array.
It's your responsibility to release the memory when you are done
free(r);
In vino veritas
|
|
|
|
|
|
You have declared r As an array of 1000 integers and then try to assign it a malloc 'ed array of elements. That makes no sense.
modified 17-Oct-16 11:07am.
|
|
|
|
|
I think I finally outdone myself.
#1 <strike>In have class member function to assign pointer to structure from another class. No matter what I do I cannot make the compiler happy.
Closes to "solution" was "need to use static" than when using static I got incomplete type, again. Originally this function was global and the pointer in question was also globally accessible ( function); The pointer ends up in globally accessible struct, so this exercise may be futile anyway. One solution may be to move the assigning function, but I am not sure of its use down the line, I am not there yet,
I am about to give up and make the function global and be done with it. </strike>
OK that one is fixed moved the pointer assinge,t function DONE
#2 I access a class function via pointer however the function calls the calling call method in sort of circular fashion. Again because originally the app was classless , flat.
Any suggestion how to solve this issue?
I really like to keep the functions / methods in relations to hardware , each in its own class.
Sorry for being such pest.
Vaclav
void HCD_SetEnumerationStartFunction(void (*ResetEnd)(void))
{
hcd_ControlStructure.ResetEnd = ResetEnd;
}
hcd->HCD_SetEnumerationStartFunction(A_USBD::USBD_GetDeviceDescriptorBegin);
-- modified 14-Oct-16 13:26pm.
|
|
|
|
|