简体   繁体   English

MinGW 中的 strstr 和 StrStrI 以及 C 中的函数作为参数

[英]strstr and StrStrI in MinGW and with function as parameter in C

What I have我拥有的

I have a weird problem.我有一个奇怪的问题。 I have a big program which reads a PCAP File.我有一个读取 PCAP 文件的大程序。 Now I want to do a search in this pcap file.现在我想在这个 pcap 文件中进行搜索。 As I mentioned this program is really big, so I have a lot of functions and a lot of parameters etc. It's a Win32 Application and I use MinGW.正如我提到的,这个程序真的很大,所以我有很多功能和很多参数等。它是一个 Win32 应用程序,我使用 MinGW。

What I want我想要的是

Now I want to perform a case sensitive search ( strstr ) or a case insensitive search ( StrStrI from Shlwapi ) depends on what the user has selected in the GUI.现在,我想执行一个区分大小写的搜索( strstr )或不区分大小写( StrStrIShlwapi )取决于哪些用户在GUI中已选择。 The search is quite huge and a lot of parameters can be set.搜索量相当大,可以设置很多参数。 So I don't want always to ask if (caseSensitve) then strstr(...) else StrStrI(...) , I want to make the decision at the beginning and I write it in a structure and the struct goes through the different functions.所以我不想总是问if (caseSensitve) then strstr(...) else StrStrI(...) ,我想在开始时做出决定,我把它写在一个结构中,这个struct通过不同的功能。

What's the Problem:有什么问题:

When I do it like shown below my program stops working after using 3 times __strstr when case insensitive is chosen (So it will run StrStrI ).当我这样做如下所示我的程序使用3次后停止工作__strstr当选择不区分大小写(因此它将运行StrStrI )。 When Case sensitive is chosen __strstr ( strstr ) works perfect.选择__strstr大小写__strstrstrstr )工作时完美。 But when I use StrStrI directly it works perfect, too ...但是当我直接使用StrStrI ,它也很完美......

I always get a warning from Eclipse:我总是收到来自 Eclipse 的警告:

assignment from incompatible pointer type [enabled by default]从不兼容的指针类型赋值 [默认启用]

But why?但为什么? LPSTR is the same as char* , isn't it? LPSTRchar*相同,不是吗? And the typedef return of the function is char* ?!而函数的typedef返回是char* ?!

This sounds all quite complicated, eh?这听起来很复杂,嗯? Here is a example code.这是一个示例代码。 This is exactly the way programmed as it is in my big program.这正是我的大程序中的编程方式。 To test it you need to add the Shlwapi library.要测试它,您需要添加Shlwapi库。

/*
 * pcap.c
 *
 *  Created on: 05.01.2015
 *      Author: Max
 */

#include <windef.h>
#include <stdlib.h>
#include <stdio.h>
#include <shlwapi.h>//need to add library shlwapi!

typedef char* (*__StrStr)(const char* str1, const char* str2);

typedef struct _findData {
    __StrStr               __strstr; //string in string function (either strstr (cs) or StrStrI (ci))
    char fCaseSensitve :1;
} findData;

void findDataTest(findData *tFindData);
void findDataTest2(findData *tFindData);

char * Test1 = "HELLO";
char * Test2 = "lo";

int main() {
    findData *tFindData = malloc(sizeof(findData));

    setbuf(stdout, NULL); //do not buffer stdout

    tFindData->fCaseSensitve = 1; //set case sensitve

    findDataTest(tFindData); //works perfect!

    tFindData->fCaseSensitve = 0; //set case insensitve

    findDataTest(tFindData); //abort after 3 times
    return 0;
}

void findDataTest(findData *tFindData) {
    if (tFindData->fCaseSensitve)
        tFindData->__strstr = strstr; //if case sensitive use strstr
    else
        tFindData->__strstr = StrStrIA; //Warning from Eclipse: assignment from incompatible pointer type [enabled by default], but why? LPSTR == char*

    findDataTest2(tFindData); //and here we test it
}

void findDataTest2(findData *tFindData) {
    __StrStr  __strstr;
    int i = 0;

    __strstr = tFindData->__strstr;

    for (; i < 10; i++) {
        printf("Test %d:\t", i);

        //if (StrStrI(Test1, Test2)) //this works like a charm!
        if (__strstr(Test1, Test2)) //this abborts after 3 times on case insenstive
            printf("str is in str!\n");
        else
            printf("nope! str isn't in str!\n");
    }
}

The StrStrI*() -functions do not use the cdecl but the stdcall calling convention. StrStrI*()函数不使用cdecl而是使用stdcall调用约定。

To ship around this you might want to introduce a wrapper around the StrStrI*() -function like this为了解决这个问题,您可能需要在StrStrI*()像这样的函数周围引入一个包装器

char * strstri(const char * s1, const char * s2) 
{
  return StrStrIA(s1, s2);
}

and to initialise the the pointer to the compare function use并初始化指向比较函数的指针使用

void findDataTest(findData *tFindData) 
{
  if (tFindData->fCaseSensitve)
    tFindData->__strstr = strstr; //if case sensitive use strstr
  else
    tFindData->__strstr = strstri;

  findDataTest2(tFindData); //and here we test it
}

As a side note: Do not use __ to prefix anything in your C code as (in most cases) this is not allowed by the C-Standard.附带说明:不要在 C 代码中使用__作为前缀,因为(在大多数情况下)这是 C 标准不允许的。

For example replace __StrStr by StrStrFunc and __strstr by pfstrstr or strstrfunc .例如,将__StrStr替换为__StrStr ,将StrStrFunc __strstrpfstrstrstrstrfunc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM